loop for cell using variable as a the input

1 view (last 30 days)
nines
nines on 5 Mar 2021
Answered: dpb on 5 Mar 2021
hello! i am trying to make a for loop for
gtm20 = gtmkm(:,:,1,[1:34])
where gtmkm is a 4-D nifti file.
So far I have tried indexing the numbers and then placing the variable in the cell, but am getting an error that says:
gtm_segmentations = {34 36}
corr_time_intervals = {20 22 }
for pet_timepoints = 1:length(gtm_segmentations)
%
gtm_fileName = ['gtm' num2str(gtm_segmentations{pet_timepoints})]; %gtm naming files
gtm_fileName = gtmkm(:,:,1,[1:{gtm_segmentations}]); %CHANGING THIS LINE
end
i've also tried:
gtm_segmentations = {[1:34] [1:36]} %38 41 44 48 53 56 63 71}
for pet_timepoints = 1:length(gtm_segmentations)
%
gtm_fileName = ['gtm' num2str(gtm_segmentations{pet_timepoints})]; %gtm naming files
%dataStruc.(gtm_fileName) = load([gtm_fileName])
gtm_fileName = gtmkm(:,:,1,[gtm_segmentations]);
savepath=[pathstem subj{k} '/kinetic_modelling_intervals/matlab_outputs/gtm_seg_time/' roi_name{r} '/'];
mkdir(savepath); %changed this dec 1st 2020
any help would be much appreciated!

Answers (1)

dpb
dpb on 5 Mar 2021
gtm_segmentations = [34 36]; % use double array, no point in cell array here
corr_time_intervals = [20 22]; % ditto
for pet_timepoints=1:length(gtm_segmentations)
gtm_fileName = num2str(gtm_segmentations(pet_timepoints),'gtm%d'); % use format string, not catenation
result=gtmkm(:,:,1,[1:gtm_segmentations(pet_timepoints)]); % refer to same array element as above
...
end
will return the two sections of the 4D array in turn -- it would seem you wouldn't want to overwrite the filename with the results of such an array operation but it's certainly unclear what part either plays in going forward...the filename is never used if it is a file and if it is, it probably also will need a fullfile() somewhere in the mix to specify the path to the file and there's no file extension in sight...
But the firstest problem is not dereferencing a cell array with {} and in not subscripting an array to return an element of the array instead of the whole thing.

Community Treasure Hunt

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

Start Hunting!