Read a .txt file and convert it into an array

3 次查看(过去 30 天)
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.

更多回答(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