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
2018-7-23
编辑: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
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!