Compile multiple matrices into one without listing them all out

1 view (last 30 days)
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

Answers (1)

Stephen23
Stephen23 on 30 May 2023
Edited: Stephen23 on 30 May 2023
"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.

Tags

Community Treasure Hunt

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

Start Hunting!