How to read a .txt file in matlab ?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
dpb
2021-1-4
编辑:dpb
2021-1-4
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!