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

9 ビュー (過去 30 日間)
Bazil Faisal
Bazil Faisal 2021 年 8 月 12 日
回答済み: TADA 2021 年 8 月 15 日
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.

回答 (2 件)

Dave B
Dave B 2021 年 8 月 15 日
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);
end
end

TADA
TADA 2021 年 8 月 15 日
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({folders.name}, {'.', '..'}));
for i = 1:numel(folders)
currFolder = folders(i);
currPath = fullfile(currFolder.folder, currFolder.name);
% call whatever function you made and send the current path
% matlabDoMyBidding(currPath);
end

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by