MATLAB Answers

Continue if file doesn't exist - pop_loadset

조회 수: 14(최근 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 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
  댓글 수: 1
bruno mansur
bruno mansur 16 Mar 2020
It works wonderfully! Thanks!

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by