Azzera filtri
Azzera filtri

loop through subjects in Matlab path to run a program in subfolders

7 visualizzazioni (ultimi 30 giorni)
I am trying to loop through multiple subjects in multiple subfolders
This is what I want to do in the long run assuming I am working weith a single subject
"%direc = dir(['/Users/konan/Documents/Data/PO1/*.HWR']);"
path1 = '/Users/konan/Documents/Data/';
path2 = 'P01/'; (I want to loop through subject P1 - P90)
path3 = '*.HWR';
direc = dir([fullfile(path1,path2, path3)]);
  3 Commenti
Reuben Addison
Reuben Addison il 11 Feb 2023
Yes the data to be processed are contained in the participant's folders eg P01, P02,,.....P090, so I want to loop through each folder
Walter Roberson
Walter Roberson il 11 Feb 2023
Is there, for example, a Pie_Recipes folder? A Photographs folder? Any other folder whose name begins with P but which is not P<digit><digit> ?

Accedi per commentare.

Risposta accettata

Yash
Yash il 2 Mar 2023
Modificato: Yash il 2 Mar 2023
Hi Reuben Addison,
To loop through multiple subjects in multiple subfolders, you can use nested loops to iterate over the subjects and subfolders. Here's an example:
path1 = '/Users/konan/Documents/Data/';
% list of subject names
subjects = {'P01', 'P02', 'P03', ..., 'P90'};
file_pattern = '*.HWR';
for i = 1:length(subjects)
% construct the path to the subject folder
subject_path = fullfile(path1, subjects{i});
% get a list of files in the subject folder that match the file pattern
direc = dir(fullfile(subject_path, file_pattern));
% loop through the files
for j = 1:length(direc)
file_path = fullfile(subject_path, direc(j).name);
disp(file_path)
end
end
In this example, we first define the root path to the data directory (path1), as well as a list of subject names (subjects) and a file pattern (file_pattern). We then loop through each subject in the subjects list, and for each subject, we construct the path to the subject folder using the fullfile function. We then use the dir function to get a list of files in the subject folder that match the file pattern. Finally, we loop through each file in the list and do something with it (e.g., read its contents, process the data, etc.).
I hope this helps.

Più risposte (0)

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by