Clear Filters
Clear Filters

How to repeat the same codes/work on different datasets in different subfolders

3 views (last 30 days)
Hi. I have a set of codes that perform a type of work on one dataset.
I would like the same set of codes to be performed on other datasets.
Each dataset is contained in different subfolders. There are 8 subfolders in total. Each one is contained in a bigger folder.
The subfolders are labelled in the format of 50Hz_sine(x) and the bigger folder is labelled as PZT + Human Activity.
Any help please. Thanks.

Answers (2)

Dave B
Dave B on 15 Aug 2021
Here's the pattern I often use for this kind of problem. What you do inside the subfolders sort of depends on what your analysis is and what's in the subfolders, but hopefully the general pattern is helpful (?)
top_fp = "D:\Master\Raw Lab Data\PZT + Human Activity"
sub_fps = dir("50Hz_sine*."); % the *. is a way to limit to folders, but you likely can just use * assuming there aren't any files with the same prefix
% storing in a cell might be useful, particularly if the results of each
% folder's analysis is a different size/shape
results = cell(1, numel(sub_fps));
for i = 1:numel(sub_fps)
% no idea what a datset looks like, say it was a bunch of csv files...
fl = dir(fullfile(top_fp, sub_fps(i).name, "*.csv"));
results{i} = nan(numel(fl), 3);
for ii = 1:numel(fl)
data = readmatrix(fullfile(top_fp, sub_fps(i).name, fl(ii).name));
results{i}(ii,1) = mean(data);
results{i}(ii,2) = std(data);
results{i}(ii,3) = numel(data);

TADA on 15 Aug 2021
make a script that inspects all subfolders of the folder your data resides in:
dataRootPath = fullfile('D:', 'Masters', 'Raw Lab Data', 'PZT + Human Activity');
% find out whatever is inside the data root path
folders = dir(dataRootPath);
% keep only folders
folders = folders([folders.isdir]);
% remove the pesky '.' and '..' folder links, thank you windows...
folders = folders(~ismember({}, {'.', '..'}));
for i = 1:numel(folders)
currFolder = folders(i);
currPath = fullfile(currFolder.folder,;
% call whatever function you made and send the current path
% matlabDoMyBidding(currPath);


Find more on Environment and Settings in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!