Mean of mutiple variables

1 view (last 30 days)
ali akbar
ali akbar on 11 Dec 2020
Commented: dpb on 11 Dec 2020
I have 5 '.mat' data files where each datafile contains 1 by 19 cell (each cell size = 512 by 9). I wanted to find the mean of the 4th column of 1st cell from all 5 data files.
Here is a look at the code.
% Extracting the 4th column
a=data1a{1,1}(:,4);b=data2{1,1}(:,4) and so on...
Now people in stoneage might used to do this kind of laborious exercise. I am wondering if anyone could give a one/two liner answer for finding mean which is not naive like mine.
ali akbar
ali akbar on 11 Dec 2020
please see attached.

Sign in to comment.

Accepted Answer

dpb on 11 Dec 2020
Edited: dpb on 11 Dec 2020
d=dir('data*.mat'); % or similar list. Name files so can use appropriate wildcard expression
for i=1:numel(d)
data=load d(i).name;
Above does the summation; same result if were to use mean in the anonymous function as well if same number elements in each cell array.
If the variables were named consistently would be easier to code; above handles whatever they are inside the .mat files as long as just one variable per file.
dpb on 11 Dec 2020
so, debug it. (Exercise for Student :) )
Accidentally wrote it as command syntax instead of function form needed.

Sign in to comment.

More Answers (0)


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!

Translated by