3

我在将使用 textread 函数的代码更改为 textscan 时遇到问题。

data.txt 的内容:(注意:我已将所有实际坐标更改为 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

代码:

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);

新代码是错误的。我在这里使用了 2 个分隔符。如果它们不为空,我想从每行中取出纬度和经度值。我该如何纠正?另外,如果文本文件中有许多不同的行,我需要做什么才能仅从以 $GPGGA 开头的行中获取 Lat Long 值?

4

1 回答 1

2

此代码应满足您的要求并放入正确的标志(请检查):

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);
于 2012-07-12T16:47:06.770 回答