필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Read a text file for a particular thing and put it in your code as a string

조회 수: 1 (최근 30일)
me
me 2015년 11월 6일
마감: MATLAB Answer Bot 2021년 8월 20일
I have a long list of GPS data that looks like this
.
.
.
$GPGGA,191313.000,3237.0423,N,08526.2246,W,1,09,0.9,241.48,M,-29.4,M,,0000*54
$GPGSA,A,3,27,15,06,18,29,09,21,26,22,,,,1.7,0.9,1.4*33
$GPRMC,191313.000,A,3237.0423,N,08526.2246,W,5.05,196.66,080211,,,A*7B
$GPGGA,191314.000,3237.0408,N,08526.2250,W,1,08,1.2,241.28,M,-29.4,M,,0000*50
$GPGSA,A,3,27,15,06,18,09,21,26,22,,,,,1.9,1.2,1.5*3D
$GPRMC,191314.000,A,3237.0408,N,08526.2250,W,5.53,189.70,080211,,,A*78
.
.
.
I only need the data for GSA, GGA, and RMC; i can just ignore the other stuff. How can I read a text document for only those rows. I think an if statement might work but I am new to matlab and unsure on how to implement it.

답변 (1개)

Walter Roberson
Walter Roberson 2015년 11월 6일
편집: Walter Roberson 2015년 11월 6일
filecontent = readfile('YourFileNameHere.txt');
wanted_lines = regexp(filecontent, '^\$GP(GSA|GGA|RMC).*$', 'match', 'dotexceptnewline', 'lineanchors');
Now wanted_lines is a cell array of strings that contains the lines you want.
  댓글 수: 2
me
me 2015년 11월 6일
what if i used text scan like this
fid = fopen('GPSUSER_933000122_20110208_140804.txt', 'r'); %Open and read the file
strg = textscan(fid, '%s'); %creat a string of each GPS sentence
fclose(fid);
how can I pick out the strings that are GSA, GGA, RMC
Walter Roberson
Walter Roberson 2015년 11월 6일
You could use that textscan() provided that there is no chance at all that there will be a space anywhere in the file. In that special case the effect would be the same as
filecontent = readfile('GPSUSER_933000122_20110208_140804.txt');
strg{1} = strsplit(filecontent, '\n');
(provided that there are no carriage returns in the file)
filecontent = readfile('GPSUSER_933000122_20110208_140804.txt');
strg{1} = regexp(filecontent, '\r?\n', 'split');
(which allows for the possibility of carriage returns)
However, if there is even one space in the file, the effect would instead be the same as
filecontent = readfile('GPSUSER_933000122_20110208_140804.txt');
strg{1} = regexp(filecontent, '\s+', 'split');
Once you have your cell array strg that has one entry which is a cell array of strings, you can use
wanted_lines = regexp(strg{1}, '^\$GP(GSA|GGA|RMC).*$', 'match');
or
wanted_lines = regexp(strg{1}, '^\$GP(GSA|GGA|RMC).*$', 'match', 'dotexceptnewline', 'lineanchors');
In this particular case where each entry of the cell array is a string representing one line, dotexceptnewline and lineanchors are redundant as they have special meaning for strings that cross multiple lines.
You might at this point notice that you have not gained anything relative to directly using
filecontent = readfile('GPSUSER_933000122_20110208_140804.txt');
wanted_lines = regexp(filecontent, '^\$GP(GSA|GGA|RMC).*$', 'match', 'dotexceptnewline', 'lineanchors');
and you will find the textscan() version to be slower.

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by