Saving variables as .mat or xls files without overwriting them with every iteration of a loop

I have written a script that loops through 15 datasets. I want to save some variables as mat files or xls files without them being overwritten on every iteration.
Any tips?
%[ALLEEG EEG CURRENTSET ALLCOM] = eeglab;
for i = 1:15
name = ['sub', num2str(i), '.set'];
EEG = pop_loadset('filename',name,'filepath','C:\\kristin\\EXAM2\\EEGrawData\\');
%% RECODING TRIGGERS
EEG = recodingTriggers(EEG);
%This function recodes the triggers into the following categories;
%correctGo, incorrectGo, falseAlarm, correctStop;
%% FILTERING
%For now, I am using the Matlab function movmean(A, k, dim) to run a
%three-point average filtering of the data. k = width of points to average.
%
EEG.data = movmean(EEG.data,3,2);
%% RE-REFERENCING;
commonAvgRef = mean(EEG.data);
EEG.data = bsxfun(@minus, EEG.data, commonAvgRef);
% Here, I first compute the average value of all electrodes, simply by computing the mean of each
% column accross the rows in the EEG.data file. Then i subtracted the vector containing
%common average reference from each corresponding column in the EEG.data matrix.
%% EPOCHING
run epochingData.m
run RTs.m
%% BASELINE CORRECTION
epochs_CG = baselineCorr(epochs_CG, -200);
epochs_CS = baselineCorr(epochs_CS, -200);
epochs_FA = baselineCorr(epochs_FA, -200);
%% ARTIFACT REJECTION
run artifactRejection.m
%% EXTRACTING ERPs
ERP_CG = mean(epochs_CG,3);
ERP_CS = mean(epochs_CS,3);
ERP_FA = mean(epochs_FA,3);
%% extracting and saving mean RTs
meanRT_CG = mean(RT_CG);
meanRT_CS = mean(RT_CS);
meanRT_FA = mean(RT_FA);
meanRT_CG = array2table(meanRT_CG);
meanRT_CS = array2table(meanRT_CS);
meanRT_FA = array2table(meanRT_FA);
writetable(meanRT_CG, 'meanRT_CS.xls'
writetable(meanRT_CS, 'meanRT_CS.xls');
writetable(meanRT_FA, 'meanRT_FA.xls');
%% Writing output
% ???
clear all
clc
end
I want to save the variables epochs_CG, epochs_FA and epochsCS (3D matrices) as separate mat files for each iteration, and the RTs as xls files. The xls files are saved, but overwritten on each iteration. Any tips?

8 Comments

Just create a filename with i appended to it each iteration e.g.
writetable(meanRT_CG, sprintf( 'meanRT_CS_%i.xls', i ) )
and do the same for mat files.
Perfect! thank you! Sorry for the dumb question, I'm a matlab novice
I wrote this up before seeing Adam's suggestion above but here's a nearly identical solution that uses an anonymous function to create the file name. By the way, why not save all of the matlab variables to the same file?
% Create an anonymous function that creates filenames
% s is the name of the file; example: 'meanRT_CS'
% d is the iteration number (integer)
% e is the file extension without the dot; example: xls
fnameFcn = @(s,d,e)sprintf('%s_%d.%s', s,d,e);
for i = 1:15
% Save mat files (but why not save all vars to same
save(fnameFcn('epochs_CS',i,'mat'), 'epochs_CS')
save(fnameFcn('epochs_FA ',i,'mat'), 'epochs_FA')
save(fnameFcn('epochs_CG ',i,'mat'), 'epochs_CG')
% Save xls files
writetable(meanRT_CG, fnameFcn('meanRT_CG',i,'xls'))
writetable(meanRT_CS, fnameFcn('meanRT_CS',i,'xls'))
writetable(meanRT_FA, fnameFcn('meanRT_FA',i,'xls'))
end
Hi, when I try to save it, I get this error;
fnameFcn = @(s,d,e)sprintf('%s_%d.%s', s,d,e);
for i = 1:15
save(fnameFcn('epochs_CG',i,'mat'), epochs_CG)
save(fnameFcn('epochs_FA ',i,'mat'), epochs_CG)
save(fnameFcn('epochs_CG ',i,'mat'), epochs_CG)
Error using save
Must be a string scalar or character vector.
Did I write it in wrong? Can't seem to get it to work.
save unfortunately only accepts the names of variables, so this is what you need to provide it:
save(fnameFcn('epochs_CG',i,'mat'), 'epochs_CG')
^ ^ must be the variable NAME!
Oops, I should have enclosed the variable names in single quotes in my examples. I'll edit it now.
Maybe put it as an official Answer below so you can get credit for it.

Sign in to comment.

 Accepted Answer

(continued from comments section above).
Here's an implementation of Adam's suggestion.
% Create an anonymous function that creates filenames
% s is the name of the file; example: 'meanRT_CS'
% d is the iteration number (integer)
% e is the file extension without the dot; example: xls
fnameFcn = @(s,d,e)sprintf('%s_%d.%s', s,d,e);
for i = 1:15
% Save mat files (but why not save all vars to same
save(fnameFcn('epochs_CS',i,'mat'), 'epochs_CS')
save(fnameFcn('epochs_FA ',i,'mat'), 'epochs_FA')
save(fnameFcn('epochs_CG ',i,'mat'), 'epochs_CG')
% Save xls files
writetable(meanRT_CG, fnameFcn('meanRT_CG',i,'xls'))
writetable(meanRT_CS, fnameFcn('meanRT_CS',i,'xls'))
writetable(meanRT_FA, fnameFcn('meanRT_FA',i,'xls'))
end

More Answers (0)

Categories

Asked:

on 22 Mar 2019

Commented:

on 23 Mar 2019

Community Treasure Hunt

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

Start Hunting!