Clear Filters
Clear Filters

Add string files to a cell array within a loop

2 views (last 30 days)
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!

Accepted Answer

Stephen23
Stephen23 on 20 Dec 2022
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'}

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!