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.

回答(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