Run a script for all subfolders
이전 댓글 표시
Hi
I have a script and I want to rut it for a folder. I should say that the folder has subfolders. The folder structure is like below:
'rocording\myproject\020320 (it is a date)\XYdata'
the first two folders are same but I have many folders for third subfolder (different dates) and one 'XYdata' folder inside each date folder.
I want to run my script for the text files inside the XYdata subfolders.
채택된 답변
추가 답변 (1개)
Adam Danz
2020년 3월 9일
Try this out,
mainDir = 'C:\Users\name\Documents\MATLAB\rocording\myproject'; % <-- replace with your real path
% Get a list of content
content = dir(mainDir);
% Remove content that isn't a subdirectory
subDirs = {content.name}';
content(~[content.isdir]' | startsWith(subDirs, '.')) = [];
% Loop through each subdirectory
for i = 1:numel(content)
% Create full path to the XYdata directory
fullpath = fullfile(content(i).folder, content(i).name, 'XYdata');
% Test that it exists
assert(exist(fullpath,'dir')==7, ['Directory doesn''t exist: ', fullpath])
% Use fullpath for your script.
% To add filename, file = fullfile(fullpath, 'myFile.mat');
end
댓글 수: 5
Benjamin Großmann
2020년 3월 9일
This is way more complex than it has to be. See my answer for the proper use of the asterisk as wildcard.
Boby S
2020년 3월 9일
Boby S
2020년 3월 9일
Adam Danz
2020년 3월 9일
Ah, if the subfolder structure isn't consistent then the wildcard method is the way to go.
카테고리
도움말 센터 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!