Best way to loop through multiple subfolders and run either script A or B depending on folder name?

3 ビュー (過去 30 日間)
Here is my current folder/subfolder set up
Study > Participant (1-24) > Experiment (A) and Experiment (B) > Session (1, 2, and 3)
  1. Folder that contains all participants data for this study
  2. Subfolder participants folder (1-24)
  3. Subfolder with data from experiment A and a subfolder for experiment B
  4. Subfolder for session 1, 2, and 3
I have a code that works at the last level - I can analyze 1 session at a time with a script designed for Experiment 1, and another script that analyzes 1 session for Experiment 2.
However this would be very time consuming to analyze each session 1 at a time.
Therefore, my I am trying to figure out how to make a loop that does the following:
  1. Loops through each participants subfolder
  2. Runs Script 1 in the Experiment A subfolder for each session
  3. Runs Script 2 in the Experiment B subfolder for each session
It's important that Script 1 does not run for Experiment B and vice versa because the data, variable names, etc are different.
Hopefully this, appreciate any help that can be offered

採用された回答

Jos (10584)
Jos (10584) 2019 年 2 月 7 日
If you have adopted a logical naming system for the files and directories, something simple like this would do:
for P = 1:24 % loop over participants
Pstr = sprintf('Participant%d',P) ;
for S = 1:3 % loop over sessions
Sstr = sprintf('Session%d', S) ;
fileA = fullfile('Study', Pstr, 'ExpA', Sstr, 'data.mat')
ResultsA (P,S) = MyAnalysisA(fileA) ; % if a single element is returned
% and something similar for B
end
end

その他の回答 (1 件)

TADA
TADA 2019 年 2 月 6 日
編集済み: TADA 2019 年 2 月 6 日
path = 'Whatever path your study folder lies in';
folders = split(genpath(path), ';');
matchExpA = cellfun(@any, regexpi(folders, '.*\\experiment \(a\)\\session.*'));
matchExpB = cellfun(@any, regexpi(folders, '.*\\experiment \(b\)\\session.*'));
% analyze experiment A
for sessionCell = folders(matchExpA)'
session = sessionCell{1}; % get the folder path of current session in experiment A
% run script for analyzing experiment A
end
% analyze experiment B
for sessionCell = folders(matchExpB)'
session = sessionCell{1}; % get the folder path of current session in experiment B
% run script for analyzing experiment B
end
If you are working on a linux system those regular expressions probably need a small change but thats the general idea

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by