How to compile the average values of a variable in .mat files

1 次查看(过去 30 天)
I have 81 .mat files that correspond to the years 1930 to 2010 and each contain a variable named 'zdaily'. In each zdaily, there are is a matrix that is 240x121x366or365 depending on if that year was a leap year or not. I'm trying to create a 'for loop' that will not only compile the .mat files into one large matrix but also take the average over the time dimension so that instead of 81x365or366, it will just be 81. Thanks
  2 个评论
Giridharan Kumaravelu
编辑:Giridharan Kumaravelu 2018-7-23
average = zeros(81,1);
for i = 1:81
data = matfile("filenameOfyear1930+i.mat");
average(i) = mean(data.zdaily,3);
end
Is this what you wanted?

请先登录,再进行评论。

回答(1 个)

Adam Danz
Adam Danz 2018-7-23
编辑:Adam Danz 2018-7-23
Let's say your mat file is called 'myMatFile.mat' and the variable of interest is 'zdaily'.
% Pull data from file
filename = 'C:\Users\me\Documents\MATLAB\myMatFile.mat';
data = matfile(filename);
zdaily = data.zdaily;
% Average over the 2nd dim
m = mean(zdaily ,2);
% Check size
size(m) %should be [1,81]
The same thing with less lines of code (faster & cleaner)
data = matfile('C:\Users\me\Documents\MATLAB\myMatFile.mat');
m = mean(matfile.zdaily,2);
  2 个评论
Paul Torres
Paul Torres 2018-7-24
There are 81 different .mat files, this code would only work for one, right?
Adam Danz
Adam Danz 2018-7-24
编辑:Adam Danz 2018-7-24
Yes, you'd need to implement a loop as Giridharan demonstrated in the comments section above. For example, you could create a cell array of all your filenames and loop through each element of the cell array.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by