How to load multiple .mat files into workspace in order to concatenate matrices and calculate mean
1 view (last 30 days)
Show older comments
Hello,
I have multiple correlation matrices (one for each person in my experiment) and am trying to create the mean correlation matrix but don't know how to do this.
I have saved each 11 x 11 correlation matrix as a .mat file and so have one for each subject (201.mat, 202.mat, 203.mat, 204.mat, etc.). The first problem I am running into is loading all of the matrices at once into the matlab workspace in order to concatenate them into a 3-D array and then be able to calculate the mean. Each time I load a new matrix it overwrites the old matrix. How do I load all the matrices into the workspace at once? And then how would I concatenate all the matrices to create a 3-D array and then compute a mean matrix?
Any help would be much appreciated. Thank you.
0 Comments
Answers (1)
Azzi Abdelmalek
on 10 Feb 2014
M=zeros(11,11,10) % for 10 files
p=0;
for k=201:210
p=p+1;
A=load(sprintf('%d.mat',k));
s=fields(A)
M(:,:,p)=A.(s)
end
1 Comment
María Maldonado Gil
on 24 Oct 2021
I don´t think this works. I´ve tried on my case and it does not run ok, as it says that is unable to find the .mat files. Don´t know why
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!