loop through subjects in Matlab path to run a program in subfolders
조회 수: 6 (최근 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개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!