saving an output with loop

2 views (last 30 days)
Ben on 19 Jul 2019
Answered: Mario Chiappelli on 19 Jul 2019
I have three subject-specific subdirectories (sub-CC121428, ...) in a main directory, 'diffusion'. I have written a `for` loop to convert a .nii file in each subdirectory to a .mat file:
practice_dir = 'C:\Work\FSL\shared\NCCN_project\diffusion'; %
subjects = [121428 420433 710429]; %
for n=1:length(subjects)
i = subjects(n);
subname = sprintf('sub-CC%06d',i);
fname_eddy = fullfile(practice_dir,subname,'dwi',sprintf('eddy_output.nii',subname));
fname_mask = fullfile(practice_dir,subname,'dwi',sprintf('b0_0.3_mask.nii',subname));
CreateROI(fname_eddy, fname_mask, 'NODDI_roi.mat')
The output is saved in the first subject subdirectory and is overwritten with each iteration. How do I save the output of each iteration (NODDI_roi.mat) in the correct subject subdirectory?
  1 Comment
infinity on 19 Jul 2019
Did you try to put the path before "NODDI_roi.mat" like
your_sub_direct_file = fullfile(practice_dir,subname,'dwi','NODDI_roi.mat');
Then, you just save your data to this path like
CreateROI(fname_eddy, fname_mask, your_sub_direct_file)

Sign in to comment.

Answers (1)

Mario Chiappelli
Mario Chiappelli on 19 Jul 2019
Save the ouput in an array based off of what iteration you are on. Something like this:
outputArray = string(length(subjects)); % I assume your output is a string
for n = 1:length(subjects)
% your code
outputArray(n) = CreateROI(fname_eddy, fname_mask, 'NODDI_roi.mat');
Hope this helps :)

Community Treasure Hunt

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

Start Hunting!