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

7 次查看(过去 30 天)
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 个评论
Reuben Addison
Reuben Addison 2023-2-11
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 2023-2-11
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> ?

请先登录,再进行评论。

采纳的回答

Yash
Yash 2023-3-2
编辑:Yash 2023-3-2
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Adding custom doc 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by