extract a variable from middle of a line of a .dat file with mixed text and numbers
Show older comments
Hello.
I'm a fairly new user of MatLab and I'm having some difficulty reading data from a .dat file.I want to extract specific values from a line of the file I'm using. That line has both text and the values i want. From the sample I'm posting bellow I want to extract the values of LAT i.e. 50 55.51 and LONG i.e. -40 26.26. I would like to get 50 55.51 -40 26.26 as different variables so I can use them to convert to decimal coordinates 50.93 and -40.44, using something like this:
lat_deg = 50 % extract from file.dat
lat_min = 55.51 % extract from file.dat
lon_deg = -40 % extract from file.dat
lon_min = 26.26 % extract from file.dat
lat = lat_deg+lat_min/60
lon = lon_deg+long_min/60
_________________sample____________________________
STATION 2 DATE 1 27 1999 TIME 14:57 TO 17:08
LAT 50 55.51 LONG -40 26.26 WATER DEPTH 4060 M
PRESS TEMP SALINITY RHO(T,S,P)
(M) (C) (PSS-78) (KG M-3)
2. 18.623 35.658 1025.35
5. 18.627 35.657 1025.56
10. 18.623 35.658 1025.79
The computers I use run Matlab R2008a, but sometimes I'll need to use the older Matlab 6.5 version. Any help is appreciated.
edited: to correct LAT/LONG values and indicate Matlab versions I'll be using
Accepted Answer
More Answers (1)
Andrei Bobrov
on 22 May 2013
f = fopen('your_data.txt');
c = textscan(f,'%s');
fclose(f);
c1 = c{:};
d = str2double(c1(bsxfun(@plus,find(ismember(c1,{'LAT' 'LONG'})),1:2)));
d(:,2) = sign(d(:,1)).*d(:,2);
out = d*[1;1/60];
1 Comment
matlab_student
on 22 May 2013
Edited: matlab_student
on 22 May 2013
Categories
Find more on Manage Products in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!