Compile multiple matrices into one without listing them all out

1 Ansicht (letzte 30 Tage)
Alice Cozens
Alice Cozens am 30 Mai 2023
Kommentiert: Stephen23 am 30 Mai 2023
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

Antworten (1)

Stephen23
Stephen23 am 30 Mai 2023
Bearbeitet: Stephen23 am 30 Mai 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!

Translated by