Add string files to a cell array within a loop

조회 수: 6 (최근 30일)
Ana Gabriela Guedes
Ana Gabriela Guedes 2022년 12월 20일
답변: Stephen23 2022년 12월 20일
Hi!!
I am creating a preprocessing fmri pipeline and wanted to ge the names of all the files from each run of a subject in a cell array. So if the subject did 3 runs I would have a 1x3 cell array where each cell would correspond to a run.
I am getting the files names in the following loop:
user = 'user';
subjects = [1]; % Replace with a list of all of the subjects you wish to analyze
runs = [1 2 3];
for subject=subjects
runFiles = cell(1,length(runs));
for run = runs
subject = num2str(subject,'%02d');
run = num2str(run);
fileName = ['C:\Users\',user,'\Documents\BIDS__dataset\sub-',subject,'\ses-01\func\sub-PL',subject,'_ses-01_task-01_run-',run,'_bold.nii'];
runFiles(run) = fileName;
end
end
But when I try to add the file name to the cell array I get the error "Conversion to cell from char is not possible."
I tried to to
runFiles(run) = cellstr(fileName);
and
runFiles{run} = fileName;
but I end up with a 1x51 cell array instead of a 1x3.
For reference, I would like to end up with something like:
{
{'C:\Users\user\Documents\BIDS__dataset\sub-PL01\ses-01\func\sub-01_ses-01_task-01_run-1_bold.nii'}
{'C:\Users\user\Documents\BIDS__dataset\sub-01\ses-01\func\sub-01_ses-01_task-01_run-2_bold.nii'}
{'C:\Users\user\Documents\BIDS__dataset\sub-01\ses-01\func\sub-01_ses-01_task-01_run-3_bold.nii'}
};
Thank you in advance!

채택된 답변

Stephen23
Stephen23 2022년 12월 20일
Simpler using COMPOSE():
user = 'user';
runs = [1,2,3];
subj = 1;
fmt = 'C:\\Users\\%s\\Documents\\BIDS__dataset\\sub-%02d\\ses-01\\func\\sub-PL%02d_ses-01_task-01_run-%d_bold.nii';
txt = compose(fmt,user,subj,subj,runs(:))
txt = 3×1 cell array
{'C:\Users\user\Documents\BIDS__dataset\sub-01\ses-01\func\sub-PL01_ses-01_task-01_run-1_bold.nii'} {'C:\Users\user\Documents\BIDS__dataset\sub-01\ses-01\func\sub-PL01_ses-01_task-01_run-2_bold.nii'} {'C:\Users\user\Documents\BIDS__dataset\sub-01\ses-01\func\sub-PL01_ses-01_task-01_run-3_bold.nii'}

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by