Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing from textread to textscan MATLAB

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?

like image 826
user1520813 Avatar asked Dec 05 '25 10:12

user1520813


1 Answers

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);
like image 172
Ansari Avatar answered Dec 07 '25 23:12

Ansari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!