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:
- dir - https://www.mathworks.com/help/matlab/ref/dir.html
- fullfile - https://www.mathworks.com/help/matlab/ref/fullfile.html
- isfolder - https://www.mathworks.com/help/matlab/ref/isfolder.html
Hope this helps!
