How to read a .txt file in matlab ?

1 回表示 (過去 30 日間)
Lars Urban
Lars Urban 2021 年 1 月 4 日
コメント済み: Lars Urban 2021 年 1 月 5 日
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);

採用された回答

dpb
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.
  1 件のコメント
Lars Urban
Lars Urban 2021 年 1 月 5 日
Thank you for the help. I will rewrite the .txt file.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by