loop through subjects in Matlab path to run a program in subfolders
5 次查看(过去 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 个评论
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
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 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!