MATLAB coding/loop question

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 Commenti

Guillaume
Guillaume il 1 Apr 2019
Modificato: Guillaume il 1 Apr 2019
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))
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
Guillaume il 1 Apr 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.
Hi Guillaume,
I'm not trying to find out if the paths exist. I have about 1400 directories, each representing a patient titled Sub0001_Ses1 all the way to Sub1400_Ses1. this code will eventuall read as i = 1: 1400. The code goes to each patient's directory and performs preprocessing steps on their MRI scans. The problem though, is that some patients have 2 function MRI scans and some have 1 in their patient directory. I'm a coding novice who doesn't know how to write a similar script that allows for this variability.
Hi Guillame,
Is there any extra information I can provide that would be helpful? Sorry, I'm just really trying to crack this problem.
Ben
A. Sawas
A. Sawas il 6 Apr 2019
Modificato: A. Sawas il 6 Apr 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
Stephen23
Stephen23 il 6 Apr 2019
Modificato: Stephen23 il 6 Apr 2019
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
Thank you both for your feedback, this is exactly what I needed!
Much appreciated.
Ben

Accedi per commentare.

Risposte (0)

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by