MATLAB Answers

Continue if file doesn't exist - pop_loadset

23 views (last 30 days)
I want my pop_events function to be executed to all '.set' files in a directory. The files are named as p[1:44]_0[1:3].set.
My code is the following:
sub = [1:44];
run = [1:3];
for i = sub
for j = run
EEG = pop_loadset('filename',sprintf('p0%i_%i.set', i,j),'filepath','.../2_correctTriggers/');
EEG = eeg_checkset( EEG );
pop_expevents(EEG, sprintf('.../p0%i_%i.csv', i, j), 'seconds');
end
end
But there are some missing files, like the p02_03.set, so the for loop returns this message when it comes to this file:
Error using load
Unable to read file
'.../2_correctTriggers/p02_3.set'.
No such file or directory.
Error in pop_loadset (line 119)
TMPVAR = load('-mat', filename);
Error in extract_log_files (line 6)
EEG = pop_loadset('filename',sprintf('p0%i_%i.set',
i,j),'filepath','.../2_correctTriggers/');
How can I write a code that skips inexistent files and continue the loop?
I would really appreciate any help,
Cheers

Accepted Answer

Jakob B. Nielsen
Jakob B. Nielsen on 16 Mar 2020
Use the exist or isfile functions.
E.g.
for i = sub
for j = run
if exist(['.../2_correctTriggers/',sprintf('p0%i_%i.set', i,j)])
EEG = pop_loadset('filename',sprintf('p0%i_%i.set', i,j),'filepath','.../2_correctTriggers/');
EEG = eeg_checkset( EEG );
pop_expevents(EEG, sprintf('.../p0%i_%i.csv', i, j), 'seconds');
else
%do nothing
end
end
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!