Read a .txt file and convert it into an array

조회 수: 3 (최근 30일)
Charles-Antoine Danis
Charles-Antoine Danis 2020년 3월 21일
댓글: Charles-Antoine Danis 2020년 3월 21일
I have to read a file.txt of data. The file.txt is written in this format:
150,1,2,3,4,5,6,7,8,9,10,
1;0.0;94.9;62.1;296.6;69.0;530.9;511.9;28.2;17.6;20.8;
2;107.4;0.0;167.3;203.7;77.4;502.4;617.1;101.3;96.9;87.4;
3;61.6;152.5;0.0;339.0;129.8;582.9;450.7;65.2;78.4;70.8;
4;296.7;204.0;356.6;0.0;279.2;426.0;806.4;288.4;286.2;274.4;414.2;
5;60.5;78.5;120.3;280.1;0.0;514.5;570.2;68.8;49.9;54.9;
the real file has 151 line by 151 column but i shorter it to be more easy to read.
I have to make an array of this data file to use easily the data in it. The first line and first column of the data file could be remove
file = fopen('MatriceDistances2.txt', 'rt');
if file ~= -1
text = fgetl(file);
while(~feof(file))
line = fgetl(file);
text = char(line,text);
end
fclose(file);
end
I thought using char to concatenate but seam doesn't work with string.
What should I do?

채택된 답변

Walter Roberson
Walter Roberson 2020년 3월 21일
readmatrix can probably handle it, with header lines set to 1. Or readtable if you do not have r2019b or later.
  댓글 수: 1
Charles-Antoine Danis
Charles-Antoine Danis 2020년 3월 21일
I owe you a bottle of wine. Thanks you a lot. You make my day!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by