using fget1 to read text file. Need help sorting data

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 Comments

What have you tried so far? Sound like it should be possible with a loop and an if.
Isn't this a duplicate question to this one?
I updated the question and marked the other one as answered
switch ele2read
case 34
gps = sort(line, 'g')
fileType(1,1)= 'GPS'
case 22
fileType(1,2)= 'GLONASS'
GLO = sort(line, 'r')
case 30
fileType(1,3)= 'Galileo'
GAL = sort(line, 'e')
case 35
fileType(1,4)='Beidou'
BDS = sort(line, 'c')
This is what I have so far. ele2read is an array that includes the number of elements in each data field. ele2read [ 34 22 30 35]. I'm getting an error with the switch case and I still think I may need to build upon using the sort function because I believe that will only store the first line with the designated character and I need to store the entire data field using the first character as a signifier. I'm a beginner with proggaming sorry in advance for my lack of background knowledge.
It would be best to have at least a representative sample of that original file uploaded here in the original format (likely as a .txt file) or possibly as a .mat file. We really don’t know what we’re working with, otherwise.
  • The file is a RINEX file and it is too large to upload here
You can certainly save a few hundred lines and attach that...
I've no klew what RINEX file is; looks like first entry is a date/time, maybe? What's its format?
I presume there's probably a header that defines the file structure? If so, reading it would undoubtedly make the parsing go much simpler.
It's a text file. Therefore you can open it with a text editor, go down a few hundred line, and then delete everything after that. Then save it as a new, now shorter, file. Then attach it here. Why can't you do that? Make it easy for us to help youi, not hard.
I'm trying. Does copy and paste change the format? The file is 7000KB so it will take awhile to select that many lines to delete. If I can copy and paste it will be alot faster. Also, do upload it as a txt file or RNX?
Here is a shortened version in a txt file. Let me know if this works
Notepad doesnt let you save as a RNX so I saved as a txt file. I copied and pasted a couple 100 lines out of the RNX file. Please let me know if I can provide anything else to help you help me
According to Walter Roberson’s Answer, a reader compatible with MATLAB already exists for RINEX .RNX files.
No reason for us to re-invent it if it works with your file.
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
"

Sign in to comment.

 Accepted Answer

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 Comments

Thanks. Yes, Matlab reads RINEX files with no problem. It's just a matter of uploading it on this site for you guys to see. I like your idea Les. I'm going to try to implement that into my code and I will respond back here once I'm done
dataGLONASS{idxGLONASS} = sprintf('%s %s', dataGLONASS{idxGLONASS}, txtLine);
Thank you for your help. Would mind explaining this line of code. I'm a little confused what the difference is between dataGLONASS and idxGLONASS and what exactly their relationship is?
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!