How to select next specific line based on string present in previous line and copy the line also from a text file?

1 回表示 (過去 30 日間)
x='$RAIM';
y='$GPGGA';
fid=fopen('myfile.txt');
while ~feof(fid)
tline = fgetl(fid);
if contains(tline,x)
fprintf(id,'%s\n',tline)
end
end
fclose(fid)
% Input file contents
$RAIM,002212,003080,000818,001156,020000,020000,000777,001098,111,3,11111111*32
$GPGGA,151943.50,1257.32717,N,07738.23927,E,2,09,0.87,823.3,M,,M,,*44.
if second last column of RAIM line is 5 not 3 then only select GPGGA line otherwise go to next line.

採用された回答

Walter Roberson
Walter Roberson 2017 年 7 月 22 日
S = fileread('myfile.txt');
gpgga_lines = regexp(S, '(?<=^\$RAIM,.*,5,[^,]*$)\$GPGGA,.*$', 'match', 'lineanchors', 'dotexceptnewline');
  12 件のコメント
POKA
POKA 2017 年 7 月 22 日
%How to write two if condition in matlab
% I want to try below logic while pulling out RAIM line having 5 in 67 position
x='$RAIM'
while ~feof(fid)
tline = fgetl(fid);
if contains(tline,x) && contains(tline,tline(:,67)==5)
fprintf(fid,'%s\n',tline)
fclose(fid)
Walter Roberson
Walter Roberson 2017 年 7 月 22 日
p1 = '$RAIM'; p2 = '$GPGGA';
selecting_data = false;
while ~feof(fid)
tline = fgetl(fid);
if ~ischar(tline); break; end; %end of file
if strncmp(tline, p1, length(p1))
selecting_data = tline(67) == '5';
fprintf(fid, '%s\n', tline);
elseif selecting_data && strncmp(tline, p2, length(p2))
fprintf(fid, '%s\n', tline);
end
end
fclose(fid);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by