extract a variable from middle of a line of a .dat file with mixed text and numbers

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

If LAT/LONG can really be anywhere in the file (not on 2nd line as shown in your sample), you could go for a regexp solution:
>> buf = fileread('myData.dat') ;
>> pat = 'LAT\s+(-?[\d\.])+\s+(-?[\d\.]+)\s+LONG\s+(-?[\d\.]+)\s+(-?[\d\.]+)' ;
>> tokens = regexp(buf, pat, 'tokens') ;
>> lat = str2double(tokens{1}(1:2))
lat =
50.0000 55.5100
>> long = str2double(tokens{1}(3:4))
long =
-40.0000 26.2600
There are thousands of ways to implement this with REGEXP; here is another possibility:
>> buf = fileread('myData.dat') ;
>> match = regexp(buf, '(?<=LAT\s+)-?[\d\.]+\s+-?[\d\.]+', 'match') ;
>> lat = str2num(match{1})
lat =
50.0000 55.5100
>> match = regexp(buf, '(?<=LONG\s+)-?[\d\.]+\s+-?[\d\.]+', 'match') ;
>> long = str2num(match{1})
long =
-40.0000 26.2600
Let me know if you want more information about how we are using REGEXP here.

4 Comments

I've only tested your solution briefly (the first example), but i think it works for me. I'll experiment more solutions latter. LAT/LONG are always on the 2nd line, as shown in the sample. Which code do you think it's best for my intentions? Yours or the one from Andrei Bobrov? Thanks for the help.
I think that you can go for simpler than both Andrei and I solutions if LAT/LONG are really always on second line, e.g.
>> fid = fopen('myData.dat', 'r') ;
>> fgetl(fid) ; % Skip 1st line.
>> coord = fscanf(fid, 'LAT %f %f LONG %f %f') ;
>> fclose(fid) ;
with that:
>> coord
coord =
50.0000
55.5100
-40.0000
26.2600
which you can easily use to compute the final longitude and latitude.
Yes, that's simple enough and does what i need. Again, thank you for your helpful input.

Sign in to comment.

More Answers (1)

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

Thank you for your input. That code works for most of what i want. But i think it won't run on some machines i use with the older MatLab 6.5 installed. Any way i can use textread or some similar function instead?

Sign in to comment.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!