MATLAB: There are two main folders which have 50 sub-folder each. In sub-folders there are tiff images. How to read images from sub-folders.

I have two folders with names M-001 and M-002 and in each of the main folder, I have sub-folders which contain tiff images. How could I read both the main folders at the same time to read all the tiff images.
Thanks.

Best Answer

% Create a cell array of pre-defined directory locations
folders = {'/media/DATA/M-001';'/media/DATA/M-002'};
for i = 1:numel(folders) % Loop through all folders in pre-defined cell array
myFolder = folders{i}; % Set folder to be processed

% Extract all sub-folders from the current folder
d = dir(myFolder); % List folder contents
idxs = [d(:).isdir]'; % Logical vector of directory contents that are folders
names = {d(:).name}'; % List of the names of all contents of the directory
folders = names(idxs); % Index into 'names' using 'idxs'
folders(ismember(folders,{'.','..'})) = []; % Remove '.' and '..' if they exist
for i = 1:numel(folders) % Loop through all sub-folders in cell array
myFolder = folders{i}; % Set folder to be processed
% Add file processing commands here
end
end