3 views (last 30 days)

Jan
on 20 Oct 2020

As Steven has mentioned already, this is the main problem:

"I have 40 matrices DJF1979_1980_125 to DJF2019_2020_125"

Do not use strange names of variables to store importand data, bevause this impedes the processing. If you load these variables from files, store them in an array instead:

List = dir('*.mat');

Data = cell(size(List));

for k = 1:numel(List)

Data{k} = load(fullfile(List(k).folder, List(k).name));

end

Then you can apply the mean() function either is another loop, or by cellfun, or concatenate the varibales in a 4th dimension at first:

AllData = cat(4, Data{:});

MeanData = mean(AllData, 3);

Stephen Cobeldick
on 20 Oct 2020

If every mat file contain exactly one variable then you can do this inside the loop:

fnm = fullfile(List(k).folder, List(k).name);

Data(k) = struct2cell(load(fnm));

Jan
on 20 Oct 2020

Opportunities for recent engineering grads.

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

Start Hunting!
## 2 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/619848-how-to-concatenate-matrices-to-calculate-mean#comment_1072908

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/619848-how-to-concatenate-matrices-to-calculate-mean#comment_1072908

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/619848-how-to-concatenate-matrices-to-calculate-mean#comment_1072953

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/619848-how-to-concatenate-matrices-to-calculate-mean#comment_1072953

Sign in to comment.