Info
この質問は閉じられています。 編集または回答するには再度開いてください。
Read a text file for a particular thing and put it in your code as a string
1 回表示 (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (1 件)
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 件のコメント
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!