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 CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by