3

I've problem changing my code that uses textread function to textscan.

Contents of data.txt:(Note:I've changed all actual coordinates to dddd.mmmmmm,ddddd.mmmmmm)

$GPGGA,104005.3,dddd.mmmmmm,N,ddddd.mmmmmm,W,1,05,4.4,73.4,M,48.0,M,,*7E
$GPGGA,104006.3,dddd.mmmmmm,N,ddddd.mmmmmm,W,1,05,2.1,73.5,M,48.0,M,,*7F
$GPGGA,104007.3,dddd.mmmmmm,N,ddddd.mmmmmm,W,1,05,2.1,74.0,M,48.0,M,,*70
$GPGGA,104008.3,dddd.mmmmmm,N,ddddd.mmmmmm,W,1,05,2.4,73.9,M,48.0,M,,*7C
$GPGGA,104009.3,dddd.mmmmmm,N,ddddd.mmmmmm,W,1,04,2.4,73.9,M,48.0,M,,*75

Code:

fid = fopen('E:\data.txt','r');
Location=zeros(2,);
Block = 1;
while(~feof(fid))
   A=textscan(fid,'%*s %*s %s %*s %s %*s %*s %*s %*s %*s','delimiter',',','delimiter','\n');
   Location(:)=[%s %s]';
   x=Location(1,:);
   y=Location(2,:);
   Block = Block+1;
end
display(Location);

The new code is wrong. I'm using 2 delimiters here. I want to take out the latitude and longitude values from each line if they are not null. How can I correct it? Also what do I need to do to take Lat Long values only from lines starting with $GPGGA if there are many different lines in the text file?

Amro
  • 123,847
  • 25
  • 243
  • 454

1 Answers1

2

This code should work for both your requirements and put in the correct signs (please check):

fid = fopen('data.txt','r');
A=textscan(fid,'%s %*s %f %s %f %s %*s %*s %*s %*s %*s %*s %*s %*s %*s','Delimiter',',');
fclose(fid);
Location = [A{[2, 4]}];
row_idxs = cellfun( @(s) strcmp(s, '$GPGGA'), A{1});
Location = Location(row_idxs, :);
LatSigns = -2*cellfun(@(dir) strcmp(dir, 'S'), A{3}(row_idxs))+1;
LongSigns = -2*cellfun(@(dir) strcmp(dir, 'W'), A{5}(row_idxs))+1;
Location = Location .* [LatSigns LongSigns];
display(Location);
Ansari
  • 8,168
  • 2
  • 23
  • 34
  • Thank you very much that solves the problem... I also need to check the 4th and 6th elements. I need to add negative signs to Lat and Long if the 4th and 6th elements are S and W respectively..Is this possible? – user1520813 Jul 12 '12 at 21:10
  • You're welcome - I updated the code to account for the signs. – Ansari Jul 12 '12 at 21:25
  • Hi I just checked this code. It doesn't work when when I have this line in the input file $GPGSV,4,4,16,27,,,,26,,,,24,,,,22,,,*79 – ZzzZZz Sep 30 '12 at 16:05