Continue if file doesn't exist - pop_loadset

4 次查看(过去 30 天)
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

采纳的回答

Jakob B. Nielsen
Jakob B. Nielsen 2020-3-16
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 EEG/MEG/ECoG 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by