How to loop through all files in subfolders in a main folder?

114 次查看(过去 30 天)
Hello,
I have a main folder and several subfolders in it. Each subfolder has several files within them. I want to create a loop in such a manner that the code would open a subfolder, do what I want it to do and then move to the next one.
Thanks in advance

采纳的回答

Stephen23
Stephen23 2018-12-28
编辑:Stephen23 2018-12-28
D = 'full path to the main folder';
S = dir(fullfile(D,'*'));
N = setdiff({S([S.isdir]).name},{'.','..'}); % list of subfolders of D.
for ii = 1:numel(N)
T = dir(fullfile(D,N{ii},'*')); % improve by specifying the file extension.
C = {T(~[T.isdir]).name}; % files in subfolder.
for jj = 1:numel(C)
F = fullfile(D,N{ii},C{jj})
% do whatever with file F.
end
end
  8 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by