How can I read text file with number of elements in each line is different?

조회 수: 4 (최근 30일)
I have an input text file:
result.txt:
0
[0
0
0
3,19,24,44,47
0.17221,0.26928,0.1476,0.2485,0.16241]
I used load function in matlab to read it (load('result.txt')), but I couldn't. How do I read this file?
Thank for your help!

채택된 답변

Walter Roberson
Walter Roberson 2016년 4월 30일
None of the standard data importing routines can handle that file because of the '[' and ']'. You could use textscan for part of it, but it probably is not worthwhile doing so. It would be easier to use fgetl() and sscanf(), or to use fileread() and regexp() .
You have not defined the desired output variables. MATLAB numeric arrays cannot have different numbers of elements per row, so if you want to preserve the structure you will need to use multiple variables or you will need to use cell arrays,
  댓글 수: 3
Walter Roberson
Walter Roberson 2016년 4월 30일
Why is the entry for 20 last when 20 is the middle series?

댓글을 달려면 로그인하십시오.

추가 답변 (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