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

4 ビュー (過去 30 日間)
Reuben Addison
Reuben Addison 2023 年 2 月 11 日
編集済み: Yash 2023 年 3 月 2 日
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 Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by