MATLAB Answers

using fget1 to read text file. Need help sorting data

11 ビュー (過去 30 日間)
Engineer_guy
Engineer_guy 2020 年 7 月 11 日
編集済み: Steven Lord 2020 年 10 月 20 日
Using the fgetl function I'm making a program to read this text file line by line. I need to be able to seperate the data entries from each other and store them in their own designated variables. You will see in the text file that some of the data entries start with a G (GPS) and other ones start with an R (GLONASS). I need to be able to get the program to recogize that first character that signifies GPS or GLONASS and then proceed to store the data entries in between. The number of data entries and lines of data are constant for both constellations. GPS has 8 lines and 34 data entries. GLONASS has 4 lines and 22 entries.
G32 2020 01 03 22 00 00 1.747743226588E-04 1.091393642128E-11 0.000000000000E+00
3.500000000000E+01-1.318750000000E+01 4.651979487904E-09 5.385931295143E-01
-7.767230272293E-07 3.395725856535E-03 9.786337614059E-06 5.153669069290E+03
5.112000000000E+05 1.862645149231E-09 1.499495835472E+00 3.725290298462E-08
9.563698279564E-01 1.892187500000E+02-2.550865203809E+00-8.079265105250E-09
-1.246480492344E-10 1.000000000000E+00 2.086000000000E+03 0.000000000000E+00
2.000000000000E+00 0.000000000000E+00 4.656612873077E-10 3.500000000000E+01
5.040180000000E+05 4.000000000000E+00
R01 2020 01 03 00 15 00 5.505327135324E-05 0.000000000000E+00 4.320000000000E+05
-1.407114599609E+04 6.327390670776E-01-0.000000000000E+00 0.000000000000E+00
-1.985983984375E+04 8.451271057129E-01-1.862645149231E-09 1.000000000000E+00
-7.659376464844E+03-3.354722023010E+00 0.000000000000E+00 0.000000000000E+00
R01 2020 01 03 00 45 00 5.505420267582E-05 0.000000000000E+00 4.338000000000E+05
-1.228681494141E+04 1.356384277344E+00-9.313225746155E-10 0.000000000000E+00
-1.796078857422E+04 1.222489356995E+00-1.862645149231E-09 1.000000000000E+00
-1.332407470703E+04-2.898560523987E+00 9.313225746155E-10 0.000000000000E+00
R01 2020 01 03 01 15 00 5.505513399839E-05 0.000000000000E+00 4.356000000000E+05
-9.198476562500E+03 2.062785148621E+00-9.313225746155E-10 0.000000000000E+00
-1.561752148438E+04 1.336973190308E+00-9.313225746155E-10 1.000000000000E+00
-1.795952929688E+04-2.218539237976E+00 1.862645149231E-09 0.000000000000E+00
R01 2020 01 03 01 45 00 5.505699664354E-05 0.000000000000E+00 4.374000000000E+05
-4.937781250000E+03 2.641427993774E+00-9.313225746155E-10 0.000000000000E+00
-1.329891015625E+04 1.200849533081E+00-0.000000000000E+00 1.000000000000E+00
-2.120773779297E+04-1.367164611816E+00 1.862645149231E-09 0.000000000000E+00
R01 2020 01 03 02 15 00 5.505792796612E-05 0.000000000000E+00 4.392000000000E+05
1.769560546875E+02 2.998314857483E+00-9.313225746155E-10 0.000000000000E+00
-1.141282763672E+04 8.685550689697E-01-0.000000000000E+00 1.000000000000E+00
-2.281775537109E+04-4.101181030273E-01 1.862645149231E-09 0.000000000000E+00
R01 2020 01 03 02 45 00 5.505885928869E-05 0.000000000000E+00 4.410000000000E+05
5.685046386719E+03 3.071467399597E+00-9.313225746155E-10 0.000000000000E+00
-1.023803710938E+04 4.269771575928E-01 0.000000000000E+00 1.000000000000E+00
-2.266500244141E+04 5.787792205811E-01 2.793967723846E-09 0.000000000000E+00
R01 2020 01 03 03 15 00 5.505979061127E-05 0.000000000000E+00 4.433700000000E+05
1.105128369141E+04 2.841253280640E+00-0.000000000000E+00 0.000000000000E+00
-9.878704101562E+03-1.961040496826E-02 9.313225746155E-10 1.000000000000E+00
-2.076089892578E+04 1.523207664490E+00 1.862645149231E-09 0.000000000000E+00
R01 2020 01 03 03 45 00 5.506072193384E-05 0.000000000000E+00 4.446000000000E+05
1.574637890625E+04 2.333828926086E+00 0.000000000000E+00 0.000000000000E+00
-1.024903222656E+04-3.674869537354E-01 9.313225746155E-10 1.000000000000E+00
-1.725210107422E+04 2.350193023682E+00 1.862645149231E-09 0.000000000000E+00
  14 件のコメント
Steven Lord
Steven Lord 2020 年 10 月 20 日
Copying question in case user edits it away.
"Using the fgetl function I'm making a program to read this text file line by line. I need to be able to seperate the data entries from each other and store them in their own designated variables. You will see in the text file that some of the data entries start with a G (GPS) and other ones start with an R (GLONASS). I need to be able to get the program to recogize that first character that signifies GPS or GLONASS and then proceed to store the data entries in between. The number of data entries and lines of data are constant for both constellations. GPS has 8 lines and 34 data entries. GLONASS has 4 lines and 22 entries.
G32 2020 01 03 22 00 00 1.747743226588E-04 1.091393642128E-11 0.000000000000E+00
3.500000000000E+01-1.318750000000E+01 4.651979487904E-09 5.385931295143E-01
-7.767230272293E-07 3.395725856535E-03 9.786337614059E-06 5.153669069290E+03
5.112000000000E+05 1.862645149231E-09 1.499495835472E+00 3.725290298462E-08
9.563698279564E-01 1.892187500000E+02-2.550865203809E+00-8.079265105250E-09
-1.246480492344E-10 1.000000000000E+00 2.086000000000E+03 0.000000000000E+00
2.000000000000E+00 0.000000000000E+00 4.656612873077E-10 3.500000000000E+01
5.040180000000E+05 4.000000000000E+00
R01 2020 01 03 00 15 00 5.505327135324E-05 0.000000000000E+00 4.320000000000E+05
-1.407114599609E+04 6.327390670776E-01-0.000000000000E+00 0.000000000000E+00
-1.985983984375E+04 8.451271057129E-01-1.862645149231E-09 1.000000000000E+00
-7.659376464844E+03-3.354722023010E+00 0.000000000000E+00 0.000000000000E+00
R01 2020 01 03 00 45 00 5.505420267582E-05 0.000000000000E+00 4.338000000000E+05
-1.228681494141E+04 1.356384277344E+00-9.313225746155E-10 0.000000000000E+00
-1.796078857422E+04 1.222489356995E+00-1.862645149231E-09 1.000000000000E+00
-1.332407470703E+04-2.898560523987E+00 9.313225746155E-10 0.000000000000E+00
R01 2020 01 03 01 15 00 5.505513399839E-05 0.000000000000E+00 4.356000000000E+05
-9.198476562500E+03 2.062785148621E+00-9.313225746155E-10 0.000000000000E+00
-1.561752148438E+04 1.336973190308E+00-9.313225746155E-10 1.000000000000E+00
-1.795952929688E+04-2.218539237976E+00 1.862645149231E-09 0.000000000000E+00
R01 2020 01 03 01 45 00 5.505699664354E-05 0.000000000000E+00 4.374000000000E+05
-4.937781250000E+03 2.641427993774E+00-9.313225746155E-10 0.000000000000E+00
-1.329891015625E+04 1.200849533081E+00-0.000000000000E+00 1.000000000000E+00
-2.120773779297E+04-1.367164611816E+00 1.862645149231E-09 0.000000000000E+00
R01 2020 01 03 02 15 00 5.505792796612E-05 0.000000000000E+00 4.392000000000E+05
1.769560546875E+02 2.998314857483E+00-9.313225746155E-10 0.000000000000E+00
-1.141282763672E+04 8.685550689697E-01-0.000000000000E+00 1.000000000000E+00
-2.281775537109E+04-4.101181030273E-01 1.862645149231E-09 0.000000000000E+00
R01 2020 01 03 02 45 00 5.505885928869E-05 0.000000000000E+00 4.410000000000E+05
5.685046386719E+03 3.071467399597E+00-9.313225746155E-10 0.000000000000E+00
-1.023803710938E+04 4.269771575928E-01 0.000000000000E+00 1.000000000000E+00
-2.266500244141E+04 5.787792205811E-01 2.793967723846E-09 0.000000000000E+00
R01 2020 01 03 03 15 00 5.505979061127E-05 0.000000000000E+00 4.433700000000E+05
1.105128369141E+04 2.841253280640E+00-0.000000000000E+00 0.000000000000E+00
-9.878704101562E+03-1.961040496826E-02 9.313225746155E-10 1.000000000000E+00
-2.076089892578E+04 1.523207664490E+00 1.862645149231E-09 0.000000000000E+00
R01 2020 01 03 03 45 00 5.506072193384E-05 0.000000000000E+00 4.446000000000E+05
1.574637890625E+04 2.333828926086E+00 0.000000000000E+00 0.000000000000E+00
-1.024903222656E+04-3.674869537354E-01 9.313225746155E-10 1.000000000000E+00
-1.725210107422E+04 2.350193023682E+00 1.862645149231E-09 0.000000000000E+00
"

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

採用された回答

Les Beckham
Les Beckham 2020 年 7 月 11 日
編集済み: Les Beckham 2020 年 7 月 12 日
The link that Walter provided looks promising.
If you wish to pursue the processing on your own, here is a script that will read the GPS and GLONASS records into separate cell arrays.
You will have to add additional code where indicated to process each of the records that you read from your data file.
% ReadRINEX.m
fp = fopen("C:\Users\LesPC\Documents\MATLAB\Answers\shortenRNX.txt", 'rt');
% number of lines of data after the 'header' line
noLinesGPS = 7;
noLinesGLONASS = 3;
idxGPS = 1;
idxGLONASS = 1;
while true
txtLine = fgetl(fp);
if (txtLine == -1)
break;
end
if (isempty(txtLine))
break;
end
if (txtLine(1) == 'G')
dataGPS{idxGPS} = txtLine; %#ok<SAGROW>
for nLine = 1:noLinesGPS
txtLine = fgetl(fp);
dataGPS{idxGPS} = sprintf('%s %s', dataGPS{idxGPS}, txtLine);
end
idxGPS = idxGPS + 1;
elseif (txtLine(1) == 'R')
dataGLONASS{idxGLONASS} = txtLine; %#ok<SAGROW>
for nLine = 1:noLinesGLONASS
txtLine = fgetl(fp);
dataGLONASS{idxGLONASS} = sprintf('%s %s', dataGLONASS{idxGLONASS}, txtLine);
end
idxGLONASS = idxGLONASS + 1;
end
end
fclose(fp);
% Use sscanf or similar here to process the records that you read from the file
fprintf('%s: Done\n', mfilename);
I've edited this because the for loops to read the lines after the 'header' were using the wrong value (idx... vs noLines...). Also, a better test for the end of the file.
  4 件のコメント
Les Beckham
Les Beckham 2020 年 7 月 16 日
idxGPS and idxGLONASS are indices that keep track of the index of how many records of either GPS or GLONASS data have been found (based on testing the first character of the record).
dataGPS and dataGLONASS are the cell arrays that accumulate the actual data in those records.
Note that I intended this only as an example of how to process a RINEX file. If your file has records that are not for GPS or GLONASS, you will need to expand on this example (that should be pretty easy, though, if you follow the pattern of this example).
To specifically answer your question regarding this line of code, see below.
dataGLONASS{idxGLONASS} = sprintf('%s %s', dataGLONASS{idxGLONASS}, txtLine);
The for loops, such as the following one,
for nLine = 1:noLinesGPS
txtLine = fgetl(fp);
dataGPS{idxGPS} = sprintf('%s %s', dataGPS{idxGPS}, txtLine);
end
read the lines of data after the first line that identifies which type of data is being read (which is detected based on the first character in the 'header' line. For example, "if (txtLine(1) == 'G')" detects that the header line starts with a'G' character and, thus, is a GPS record which, since noLinesGPS is 7, requires reading 7 more lines of data.
I hope this explains things.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 7 月 11 日
編集済み: Walter Roberson 2020 年 7 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by