How to load multiple .mat files into workspace in order to concatenate matrices and calculate mean

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

Answers (1)

Azzi Abdelmalek
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

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!