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

23 vues (au cours des 30 derniers jours)
Suat Ching Lau
Suat Ching Lau le 9 Avr 2020
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.

Réponses (1)

Srivardhan Gadila
Srivardhan Gadila le 11 Avr 2020
Modifié(e) : Srivardhan Gadila le 11 Avr 2020
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

Community Treasure Hunt

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

Start Hunting!

Translated by