How to read a .txt file in matlab ?
2 views (last 30 days)
Show older comments
How can i read this file into a array ,
1 00000U 00000A 20312.59332066 -.00000053 +00000-0 +00000-0 0 9995
2 00000 041.7516 141.5756 0748458 270.4086 349.5770 01.00280009037209
I used this code :
fileID = fopen('TLE.txt','r');
formatSpec = '%d %c ';
sizeA = [2 inf];
A = fscanf(fileID,formatSpec,sizeA);
fclose(fileID);
0 Comments
Accepted Answer
dpb
on 4 Jan 2021
Edited: dpb
on 4 Jan 2021
The file is irregular -- there are 9 elements in first record and only eight (8) in second. Besides that, they're inconsistent in data type by position in the second and third columns.
You need a definition of what the file contains to be able to parse it.
readtable would bring it in with cellstr data types for the mismatched columns and a missing value, but you would still need to know "who's who in the zoo" in order to make any sense of what the file contains.
>> tlars=readtable('lars.txt','ReadVariableNames',0,'HeaderLines',0)
tlars =
2×9 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9
____ __________ ____________ ________ _________ ____________ ____________ ____ _______
1.00 {'00000U'} {'00000A' } 20312.59 -0.00 {'+00000-0'} {'+00000-0'} 0.00 9995.00
2.00 {'00000' } {'041.7516'} 141.58 748458.00 {'270.4086'} {'349.5770'} 1.00 NaN
>>
returns everything in the file plus the missing variable indicator for the second record; what you can make out of it is up to you having some idea of what the various pieces are.
NB: also that MATLAB doesn't recognize the floating point values as being so missing the E in the exponent; you'll have to work around that as well.
More Answers (0)
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!