MATLAB: Can I use fullfile() to read two subfolders

fullfile

Hi,
Im wondering is it possible to use fullfile to read two subfolder for further specific file extraction? For example, I have two subfolder, named 'Faces' and 'NonFaces', that are located in the main folder 'images'.
Main_Folder= 'D:\my_mfiles';
cd(Main_Folder)
subfolder=fullfile(Main_Folder, 'Faces','NonFaces') % Here went wrong
cd(subfolder)
I'd also tried this way, but it didnt work:
subfolder='Faces''NonFaces';
Or there is a better solution to read two subfolders?
Any suggestion is much appreciated!

Best Answer

Hello Yanagawa,
You should use for loop for that operation to get one by one each folder path.
Try this (also detects all folders in a parent folder). You can create a cell array of folder names you wish in folders variable.
Main_Folder = 'C:/users';
files = dir(Main_Folder);
idx = [files.isdir];
folders = {files(idx).name}; % this can be your folders = {'Faces','NonFaces'}
% If You can also only define folders you don't need above code.
subfolder = cell(1,numel(folders));
for i = 1:numel(folders)
subfolder{i} = fullfile(Main_Folder, folders{i})
cd(subfolder{i}) % Change your directory to i'th subfolder
% do what you want (your file extraction, etc) in that current directory
end