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

23 views (last 30 days)
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
Adam Danz
Adam Danz on 23 Mar 2019
Oops, I should have enclosed the variable names in single quotes in my examples. I'll edit it now.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 23 Mar 2019
(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

Find more on Get Started with MATLAB 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!