Reading, finding and storing the lines from comma-separated text file into a matrix

1 次查看(过去 30 天)
I have some "raw measurements (COOR)" data within a comma-separated text file, where there is no order, as shown below:
Nav,93,769,1,5,2,16,-61,0,58,22,-84,96,22,-62,87,-56
Nav,95,769,1,5,2,20,-61,0,-3,7,68,65,-50,-82,-55,-40
Nav,98,769,1,5,2,16,-61,4,28,-11,108,-120,-101,36,75,-96
Raw,15936970,1049265000000,,,-1211809192735061084,0.0,2.9654227761014607,,,0,4,0.0,47,395841933057947,13,36.97992706298828,-25.2641683469911,0.0683774000209613,1,-81733.38959449297,0.0012863802257925272,,,,,0,,1,,
Raw,15936970,1049265000000,,,-1211809192735061084,0.0,2.9654227761014607,,,0,5,0.0,47,395841918979605,11,37.945770263671875,288.1463114369699,0.06249389100097444,1,237817.0377973174,0.0011510049225762486,,,,,0,,1,,
Raw,15936971,1049265000000,,,-1211809192735061084,0.0,2.9654227761014607,,,0,16,0.0,12,395841926335087,1000000000,8.595783233642578,-512.0802523112693,2.99792458E8,4,-560764.5256884822,3.4028234663852886E38,,,,,1,,1,,
Raw,15936971,1049265000000,,,-1211809192735061084,0.0,2.9654227761014607,,,0,20,0.0,13,395841920574430,37,28.10095977783203,-620.521767528156,0.18721956010700125,1,-445784.04978700366,0.003370865946635604,,,,,0,,1,,
Fix,gps,45.478482,9.225768,157.352068,0.054409,4.000000,1527775024000
NMEA,$GPGGA,135704.00,4528.708943,N,00913.546103,E,1,15,0.4,109.3,M,48.1,M,,*6A <-------------
,1527774995151
NMEA,$PGLOR,9,STA,135704.00,0.002,0.000,33,1,4,0,P,F,L,0,C,3,S,00080042,325,5,R,000832F4,TpeF,17,1049265,LC,,,*78
,1527774995151
Fix,gps,45.478483,9.225768,157.352055,0.008226,4.000000,1527775025000
NMEA,$GPGSV,3,1,10,21,71,174,33,26,67,300,38,29,43,066,40,16,36,308,10*70
,1527774996155
NMEA,$GPGGA,135705.00,4528.708963,N,00913.546079,E,1,15,0.4,109.3,M,48.1,M,,*65 <-------------
,1527774996155
NMEA,$GPGSV,3,2,10,31,35,215,28,25,21,131,33,20,18,149,30,05,13,041,38*7B
,1527774996155
Raw,15938107,1050265000000,,,-1211809192735061051,0.0,3.0030861848421377,,,0,4,0.0,47,395842933058035,19,33.758819580078125,-24.963595162916434,0.09802139458696979,1,-81758.4301722202,0.0018639093032106757,,,,,0,,1,,
Raw,15938108,1050265000000,,,-1211809192735061051,0.0,3.0030861848421377,,,0,5,0.0,47,395842918978641,13,36.676841735839844,288.3872062396731,0.07178706438877874,1,238105.3361227661,0.0013320597354322672,,,,,0,,1,,
....
I need to write a program which reads each line, searches if the line starts with NMEA,$GPGGA (it is highlighted by "<----" in code) and stores 3., 4., 6. columns as a matrix. For example:
[135704.00 4528.708943 00913.546103;
135705.00 4528.708963 00913.546079;
... ;
... ]
I was planing to use fgetl program, but I don't know how to save datas into a matrix:
fid = fopen('COOR.txt');
tline = fgetl(fid);
tlines = cell(0,1);
while ischar(tline)
tlines{end+1,1} = tline;
tline = fgetl(fid);
end
fclose(fid);
Any suggestions would be greatly appreciated! :)

回答(1 个)

Paolo
Paolo 2018-6-12
编辑:Paolo 2018-6-12
The best way to parse your .txt file is probably regexp.
corr = fileread('CORR.txt');
tokens=regexp(corr,'(?<=NMEA,\$GPGGA,)(\d*.?\d*),(\d*.?\d*),\w,(\d*.?\d*)','tokens');
corrmat = cell2mat(cellfun(@(x) str2double(x),tokens','un',0));
The expression:
  • Lookbehind for character vector 'NMEA,$GPGGA'
  • Match any number of digits
  • Match '.' optionally
  • Match any number of digits
  • Match a comma
  • Match any number of digits
  • Match '.' optionally
  • Match any number of digits
  • Match a comma, a word character, and a comma
  • Again, any number of digits
  • Match '.' optionally
  • Match any number of digits
The result is the tokens captured by groups one, two and three, so the three numerical values.
Corrmat:
corrmat =
[135704 4528.708943 913.546103
135705 4528.708963 913.546079]
You can check the expression above here. You can also play around with regexes in Matlab with Stephen's really cool interactive tool, which you can find on the file exchange here.

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by