How do I run a loop through multiple subjects?

조회 수: 5 (최근 30일)
Troy Tyszkowski
Troy Tyszkowski 2020년 3월 20일
댓글: Robert U 2020년 3월 20일
Hello.
I'm sure this question has been asked a fair amount, and I've been attempting to look up the answer on various forums and tutorials, but they don't seem to answer my specific question.
If I have a group of subjects that I want to run a matlab command for, I understand how to set the subjects up as an array (subject_list = {'subject1','subject2'}), but if I am then attempting to run the command through these subjects, how would I enter the new 'subject_list' variable into the command? The command I have is:
roast('subject1/mri/T1.nii') where 'roast' is the command, and /mri/T1.nii is the same for each subject, so the only thing that would be changing is 'subject1' where I would like to loop all of my subjects.

채택된 답변

Robert U
Robert U 2020년 3월 20일
Hi Troy Tyszkowski,
loop through your subject list (replace the disp()-command by your 'roast'):
subject_list = {'subject1','subject2'};
for nSubject = 1:numel(subject_list)
cmdString = strcat( subject_list{nSubject},'/mri/T1.nii');
disp(cmdString)
end
Alternatively, use the cellfun()-function to shorten your code. Not so nice to read and debug, but quite quick in execution:
cellfun(@(cIn) disp(strcat(cIn,'/mri/T1.nii')),subject_list)
If you use paths you might want to look into fullfile() and filesep().
Kind regards,
Robert
  댓글 수: 2
Troy Tyszkowski
Troy Tyszkowski 2020년 3월 20일
Hello Robert.
Thank you very much for your help! This is gonna cut down on my time by a large margin!
I'm not sure if it's already built into that code, but the 'roast' process doesn't work with every subject. Is there anything I would need to add to the code in order for it to not stop when there is an error and just move on to the next subject?
Robert U
Robert U 2020년 3월 20일
Therefore, you can use try, catch-statement. Just leave the catch part empty or fill a warning in to keep track what subject did not work.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by