MATLAB: How to read image from folder with many subfolder directories

MATLABread images from sub folders

I need to read images from many subfolders in a fixed folder for further processing of image in a loop.
For better understaning a flowchart is given here.
My matlab code and datasetImage folder are in same folder.I need to read image .tif image in every subfolders.For note there are some folders which is missing like IM005.It will be very helpfull if anyone can help me to read ***.TIF file for further processing.Thanks in advance.

Best Answer

Try this:
% Get a list of all TIFF files in the current folder AND in all subfolders below it.
fileListing = dir('**/*.tif*');
% For each file we learned of, process it...
numberOfFiles = length(fileListing)
for k = 1 : numberOfFiles
% Get the full filename (folder & base file name) of this particular file.
thisFullFileName = fullfile(fileListing(k).folder, fileListing(k).name);
fprintf('Processing image %d of %d : %s...\n', k, numberOfFiles, thisFullFileName);
% Now do something with this image. For example, display it:
% First read image into a variable.
thisImage = imread(thisFullFileName);
% Now display it.
imshow(thisImage);
drawnow; % Force immediate refresh of screen.
% Now perform other operations on the image if you want...
end