Compile multiple matrices into one without listing them all out
1 次查看(过去 30 天)
显示 更早的评论
Heya,
I am trying to put a series of matrices into one file. I can do this by writing each of the number in one by one (but there are lots). Is there a quicker way to do this?
My code so far:
numfiles = 60;
mydata = cell(1,numfiles);
for k = 1:numfiles
myfilename= sprintf('40nMket (%d).txt',k);
mydata{k} = importdata(myfilename);
end
%combine
fulldata = [mydata{1,1}.data, mydata{1,2}.data, mydata{1,3}.data, mydata{1,4}.data, mydata{1,5}.data, mydata{1,6}.data]
^how can I do the above final line without typing all numbers up to 60?
Many thanks
0 个评论
回答(1 个)
Stephen23
2023-5-30
编辑:Stephen23
2023-5-30
"how can I do the above final line without typing all numbers up to 60?"
The general approach is to use a comma-separated list:
numfiles = 60;
mydata = cell(1,numfiles);
for k = 1:numfiles
myfilename= sprintf('40nMket (%d).txt',k);
mydata{k} = importdata(myfilename).data; % changed
end
fulldata = [mydata{:}]
I strongly recommend that you replace IMPORTDATA with READMATRIX.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 String Parsing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!