Can I use fullfile() to read two subfolders ?

9 views (last 30 days)
Emma
Emma on 16 Nov 2018
Commented: Emma on 16 Nov 2018
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!
  2 Comments
Jan
Jan on 16 Nov 2018
Edited: Jan on 16 Nov 2018
Of course you cannot set the current directory to two subfolders at the same time. There is one current directory only.
'Faces''NonFaces'
This creates the CHAR vector Faces'NonFaces with a single quote inside. Do not start to try guessing, what Matlab does, but read the example code in the documentation, e.g.:
doc fullfile
Stephen23
Stephen23 on 16 Nov 2018
Note that calling cd in your code is slow and makes debugging harder. It is recommended to simply pass the full filename to whatever function is reading/writing files. All MATLAB functions accept a relative/absolute filename, so this is what you should be doing.

Sign in to comment.

Accepted Answer

Luna
Luna on 16 Nov 2018
Edited: Luna on 16 Nov 2018
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
  2 Comments
Jan
Jan on 16 Nov 2018
+1. This works fine. I'd suggest not to call cd to change the current directory, because this can happen in each callback of timer 's or GUIs also. Better use absolute filenames created by fullfile again.
Emma
Emma on 16 Nov 2018
Thanks for the explnations and asnwers, this works fine!

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 16 Nov 2018
fullfile(A,B,C) constructs [A filesep B filesep C]
fullfile is only about taking strings and mechanically putting them together as if they were parts of a qualified file name . It is a string manipulation routine .
You can never cd to two folders simultaneously . You can cd to subfolders but you can only be active in one folder at a time .
If you want to extract from multiple folders then fetch the names from one and then fetch the names from the other and put the two groups together .

Categories

Find more on Install Products in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!