How can I read text file data separately row by row into different array

조회 수: 18 (최근 30일)
Suat Ching Lau
Suat Ching Lau 2020년 4월 9일
편집: Srivardhan Gadila 2020년 4월 11일
Hi, everyone. If the text tile data is shown as below:
13,7,8,4,9
3,21,5,3,7
4,6,87,35,7
45,2,5
I would like to read the text file into different array like a=[13 7 8 4 9], b=[3 21 5 3 7;4 6 87 35 7] and c=[45 2 5]. Is there any code to solve this problem? Thank you for reading my question.

답변 (1개)

Srivardhan Gadila
Srivardhan Gadila 2020년 4월 11일
편집: Srivardhan Gadila 2020년 4월 11일
Refer to importdata & str2num. Create a for loop based on the number of lines in the text file.
The following code might help you:
filename = 'textFile.txt';
delimiterIn = '\n';
Data = importdata(filename,delimiterIn);
a = str2num(Data{1})
b = [str2num(Data{2});str2num(Data{3})]
c = str2num(Data{4})
Or
filename = 'textFile.txt';
delimiterIn = ',';
Data = importdata(filename,delimiterIn);
a = Data(1,:)
b = Data(2:3,:)
c = Data(4,:)
c = c(~isnan(c))
The textFile.txt has the following data:
13,7,8,4,9
3,21,5,3,7
4,6,87,35,7
45,2,5

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by