-1

I have several .mat files, which are all in a 3-D matrix of latitude x longitude x value (70 x 70 x 8760).

It looks like this: year2000.mat --> (72 x 70 x 8760), year2001.mat --> (72 x 70 x 8760),until year 2020

I need the long-term mean along the third dimensions-the value. The result should be again a .mat file with 3-Dimension (72 x 70 x 8760). The first and second dimensions don't change.

I am very new to Matlab.

Lisa
  • 35
  • 4
  • 1
    what have you tried? where did you get stuck ? – bla Jun 29 '22 at 10:18
  • files = dir('*.mat'); for file = files' mat = load(file.name); variable =mat.variable; m = mean(variable ,3) end ... but this is not doing the long-term mean – Lisa Jun 29 '22 at 11:20
  • great! I'd edit this info into the question I – bla Jun 29 '22 at 13:41

1 Answers1

2

you did well in loading the files but since you know all the files start with year you can do:

fn = dir('year*.mat');

Then, you load the files, but if I understand you correctly you want to mean the years, not each year. so,

data = zeros(72,70,8760);
for n=1:numel(fn)
      single_year = load(fn(n).name);
      data = data + single_year.variable ; 
 end

is variable really the name if the variable in each mat file? I just used what you wrote, but you should check what you get if you load a single file.

now the avg is just the sum over the n:

 data=data./numel(fn);
bla
  • 25,846
  • 10
  • 70
  • 101