Looping Through Multiple Paths

2 visualizaciones (últimos 30 días)
Naomi Gaggi
Naomi Gaggi el 15 de Oct. de 2018
Respondida: ag el 23 de Abr. de 2025
I am trying to create a code that runs through multiple paths/different folders, within one large mother directory. The folders have the names Users 1, Users 2, Users 3, etc in the mother directory Group Analysis. I want to go 7 subfolders below Users 1, etc. I then want to create a for loop in which I execute commands in each of the subfolders consecutively. I have only successfully wrote a program for one path only:
s= {
{'/Users/Naomi/Desktop/GroupAnalysis/Users/jk/Documents/LongitudinalR01/Imaging/raw/c001/s1/0007_t1_mpr_AX_MPRAGE'},
s_out = {'/Users/Naomi/Desktop/output'};
for kk = 1:numel(s);
t = spm_select('FPList', (kk), '.*');
hdr = spm_dicom_headers(t);
spm_dicom_convert(hdr, 'all', 'flat', 'nii', char(s_out(kk)))
end
Thanks!!

Respuestas (1)

ag
ag el 23 de Abr. de 2025
Hi Naomi,
To process multiple directories within a "mother directory" and executing commands in each of the subfolders consecutively, you can use the MATLAB function "dir" to list all directories within and then execute the respective commands.
The below code snippet demonstrates how to achieve the same:
% Get a list of all user directories in the motherDir
userDirs = dir(fullfile(motherDir, 'Users*'));
% Loop over each user directory
for i = 1:length(userDirs)
if userDirs(i).isdir
userDirPath = fullfile(motherDir, userDirs(i).name);
% Navigate 7 subfolders deep
subDirPath = fullfile(userDirPath, 'jk/Documents/LongitudinalR01/Imaging/raw/c001/s1/0007_t1_mpr_AX_MPRAGE');
% Check if the subdirectory exists
if isfolder(subDirPath)
% Define the output directory
s_out = '/Users/Naomi/Desktop/output';
% Execute the respective command in the subdirectory, subDirPath
end
end
end
For more details, please refer to the following MathWorks documentations:
Hope this helps!

Categorías

Más información sobre Search Path en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by