Average and plot variable from multiple .mat files

5 views (last 30 days)
Kristin
Kristin on 30 Nov 2018
Edited: dpb on 30 Nov 2018
Hi! I am trying to average several variables contained in multiple .mat files. The variables I would like to perform a grand average on are 'ersp' and 'itc'. I would then also like to plot a figure for each of the average across all the files, with respect to the 'hemisphere' and the 'ROI'. However, when I try to take a squeeze(mean(ersp)) this does not work. The variables 'ersp' and 'itc' are arrays with values like (25 x 200 x 64) however, this last value changes depending on the files (it corresponds to the number of channels which is different per file).
Here is my script so far:
saving =[OPT.SAVEFOLDER];
mkdir(saving); cd(saving);
save(['DATA_' patient '_' hemisphere '_' timepoint '_' OPT.ROIS{roi} '.mat'],'EEG','ersp','itc','powbase','times','frequencies','-v7.3');
myFolder = [OPT.SAVEFOLDER];
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, 'DATA_*.mat');
matFiles = dir(filePattern);
for k = 1:length(matFiles)
matFilename = fullfile(myFolder, matFiles(k).name)
matData = load(matFilename);
end
for k = 1:length(matFiles)
matFilename = fullfile(myFolder, matFiles(k).name)
matData = load(matFilename);
hasField = isfield(matData, 'ersp');
if ~hasField
warningMessage = sprintf('ersp is not in %s\n', matFilename);
uiwait(warndlg(warningMessage));
continue;
end
ersp = matData.ersp; % Extract ERSP %
figure; plot(squeeze(mean(ersp,'omitnan')));
end

Answers (1)

dpb
dpb on 30 Nov 2018
Edited: dpb on 30 Nov 2018
The first loop over k below is superfluous...it just loads each file into the same array but does nothing with it; then you start all over again immediately. Just delete that loop entirely.
for k = 1:length(matFiles)
matFilename = fullfile(myFolder, matFiles(k).name)
matData = load(matFilename);
end
for k = 1:length(matFiles)
matFilename = fullfile(myFolder, matFiles(k).name)
matData = load(matFilename);
...
Then, you need two things -- an accumulator for the means across the number of files by variable and to compute the mean for the entire array if, as it sounds, that is the wanted statistic--that needs the (:) Matlab idiom to return the full array regardless of dimensions/size as a single (column) vector:
vars={'ersp','itc'}; % variables wanted from .mat file
nF=length(matFiles); % how many files found?
mns=nan(nF,length(vars)); % allocate space; NaN so if missing won't plot 0
for k=1:nF
matFilename = fullfile(myFolder, matFiles(k).name); % fully-qualified file name
s=load(matFilename vars{:}); % load the desired variables in struct s
mns(k,:)=structfun(@(x) mean(x(:),'omitnan')).'; % compute means by file
end
Leaves you with the array of means for the variables in vars requested to plot as desired.
  2 Comments
Kristin
Kristin on 30 Nov 2018
Thank you for your help!!
A few questions, so I can learn..
  1. what is the mns function?
  2. Does the last line work as is? When I ran it I got several errors, about the unexpected (i:) for example. I'm very new to this platform, so I'm not entirely sure how to fix it.
dpb
dpb on 30 Nov 2018
mns isn't a function; it's an array allocated to collect the computed means.
Oh, pooh! I see a couple of typos in the last line; the LHS needs to have a comma separating the row index from the column : and I pasted from command line to the editor and had used i instead of k as a variable. The line should be
mns(k,:)=structfun(@(x) mean(x(:),'omitnan')).';

Sign in to comment.

Categories

Find more on Variables 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!