MATLAB: Searching Windows folders

dirfolderssearch

Hi, I am trying to produce a code which will search for photographs within a folder, this folder however has a number of sub folders.
In my initial attempt at this problem I used the "dir" funtion to find all the folder paths and then searched all the folder paths I found for 'JPEG' files in a for loop after all the paths have been found.
The process used to find the sub folders goes: 1) search 'photograghs' folder for sub folders (using dir) 2) "for" loop through all the sub folders found 3) search for folders within each sub folder. (using dir) 4) …… continues till the maximum depth of subfolders
This approach essentially produces a big nest of for loops to find the sub folders and seems to be a very backwards approach, sadly I could not think of another way.
I am however not finding the correct number of sub folders. I would be very grateful for any guidance. Thanks James

Best Answer

As usual for standard problems, there are several submissions in the FileExchange, because others have faced the same problem before. So simply search there.
You do not need nested loops, when you call your function recursively for each folder.