How can I read text file data separately row by row into different array
17 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
Srivardhan Gadila
2020-4-11
编辑:Srivardhan Gadila
2020-4-11
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!