MATLAB coding/loop question
Mostrar comentarios más antiguos
Hi there, I have a coding quesion! I'm writing a scrip for neuroimaging analysis. Some subjects have 1 functional scan, some have 2, it's about 50-50 and there's no way to predict. Is there any easy way to allow my code to be flexible for this? My code is below
for i = 1:2
for j = 1:2
filename = sprintf('/media/nir/SharedDrive/Ben/BATCHtester1/Sub014%d_Ses1/Sub014%d_Ses1_Scan_0%d_BOLD%d.nii', i, i, j + 1, j)
BATCH.Setup.functionals{i}{j}= filename;
clear filename;
end
end
Thank you!
8 comentarios
Some subjects have 1 functional scan, some have 2
What does that mean in the context of your code? The code you show build paths and that's it. We need more explanations.
Also, in general you should never use clear (and co.). In the loop you've written, the only thing it's doing is slowing matlab.
Note that your loop could be replaced by:
[ii, jj] = ndgrid(1:2, 1:2); %better variable names needed
filenames = compose(sprintf('/media/nir/SharedDrive/Ben/BATCHtester1/Sub014%d_Ses1/Sub014%d_Ses1_Scan_0%d_BOLD%d.nii', ii(:), ii(:), jj(:)+1, jj(:)))
BATCH.Setup.functionals = reshape(filenames, size(ii))
Guillaume
el 1 de Abr. de 2019
Benjamin Davidson's comment mistakenly posted as an answer moved (and reformated) here:
Thanks for the reply! yes this code is just intended to build a path. Basically it's part of a larger code that goes through a large database of patient MRI scans, building a path to each patient's scans. In the code I have there, j = 1:2 indicates that many of the patients have 2 MRI's in their indirectory, but some of them actually have only 1.
Here's the entire code, and thanks in advance for any help!
clear BATCH;
BATCH.filename= '/media/nir/SharedDrive/Ben/BATCHtester1/conn_2.mat';
BATCH.Setup.isnew=1;
BATCH.Setup.nsubjects=2;
for i = 1:2
for j = 1:2
filename = sprintf('/media/nir/SharedDrive/Ben/BATCHtester1/Sub014%d_Ses1/Sub014%d_Ses1_Scan_0%d_BOLD%d.nii', i, i, j + 1, j)
BATCH.Setup.functionals{i}{j}= filename;
clear filename;
end
end
for i = 1:2
filename = sprintf('/media/nir/SharedDrive/Ben/BATCHtester1/Sub014%d_Ses1/Sub014%d_Ses1_Scan_01_ANAT1.nii', i, i)
BATCH.Setup.structurals{i}= filename;
clear filename;
end
BATCH.Setup.preprocessing.steps={'default_mni'};
BATCH.Setup.preprocessing.fwhm=8;
BATCH.Setup.preprocessing.voxelsize_func=2;
BATCH.Setup.preprocessing.sliceorder=[1:2:47,2:2:47];
BATCH.Setup.RT=3.0;
BATCH.Setup.analyses=[1,2];
BATCH.Setup.voxelmask=1;
BATCH.Setup.voxelresolution=1;
BATCH.Setup.outputfiles=[0,1,0];
BATCH.Setup.roi.names={'LeftVentralStriatum','RightVentralStriatum', 'LeftDorsalPutamen', 'RightDorsalPutamen', 'LeftMedialDorsalThalamus', 'RightMedialDorsalThalamus', 'LeftVentralPutamen', 'RightVentralPutamen', 'LeftDorsalStriatum', 'RightDorsalStriatum'};
conn_batch(BATCH);
Guillaume
el 1 de Abr. de 2019
I'm still not very sure what you're asking. Are you trying to find out if the paths you have built actually exist?
That's certainly doable. However, I'd approach the other way round, rather than building a list of path and checking that they exist, I would simply get the list of valid paths under '/media/nir/SharedDrive/Ben/BATCHtester1', either the whole list of .nii files in all subdirectories, or, if there are .nii files that are not supposed to be processed, just the ones that conform to your directory/file pattern.
Benjamin Davidson
el 1 de Abr. de 2019
Benjamin Davidson
el 5 de Abr. de 2019
You can approach this problem by listing the scans in each folder using the dir command. So you would write somthing like this:
n_patients = 1400;
for i = 1:n_patients
patient_dir_name = sprintf('/media/nir/SharedDrive/Ben/BATCHtester1/Sub014%d_Ses1/', i);
nii_files = dir(fullfile(patient_dir_name, '*.nii'));
for j = 1:length(nii_files)
filename = fullfile(patient_dir_name, nii_files(j).name);
end
end
Simplify the code by defining the root path before the loop:
N = 1400;
R = '/media/nir/SharedDrive/Ben/BATCHtester1';
for ii = 1:N
D = sprintf('Sub014%d_Ses1', ii);
S = dir(fullfile(R,D,'*.nii'));
for jj = 1:numel(S)
F = fullfile(R,D,S(jj).name);
... your code
end
end
Benjamin Davidson
el 8 de Abr. de 2019
Respuestas (0)
Categorías
Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!