MATLAB: How to go through multiple subfolders for operation

multiple folders

I only can access into one folder and process the data inside that folder. My Code is like this
%Specifying file directory for Ignition delay time
IGNfolder = '/home/ali//Curve_Matching_vs_Moments/IDT/test/Test_rig';
% It checks whether the folder actually exists or not. Warning message to the user if not.
if ~isdir(IGNfolder)
errorMessage = sprintf('Error: The following folder is not found:\n%s', IGNfolder);
uiwait(warndlg(errorMessage));
return;
end
% To get all the files in that directory and with desired file name pattern.
Allfiles = dir(fullfile(IGNfolder, '*.txt'));
allData = [];
for k = 1:length(Allfiles)
initialFileName = Allfiles(k).name;
fullFileName = fullfile(IGNfolder, initialFileName);
.
.
.
end
If i have multiple such kind of folder (Test_rig), How can i access into them one after another ?
For better explaining if i have one folder named 'JSR' which contains subfolders name " '1', '2', '3', '4'………………….., 'n' " .
N.B: There are other subfolders also, but i only need to access into subfolders named " '1', '2', '3', '4'………………….., 'n' " .

Best Answer

"For better explaining if i have one folder named 'JSR' which contains subfolders name '1', '2', '3', '4'......................., 'n' "
D = 'path to directory JSR';
S = dir(fullfile(D,'*'))
X = [S.isdir] & ~cellfun('isempty',regexp({S.name},'^\d+$','once'));
N = natsortfiles({S(X).name}) % optional: download from FEX.
%N = {S(X).name};
for k = 1:numel(N)
F = fullfile(D,N{k}) % path to subdirectory.
... your code
end
You can download natsortfiles here: