Can I use fullfile() to read two subfolders ?

9 visualizaciones (últimos 30 días)
Emma
Emma el 16 de Nov. de 2018
Comentada: Emma el 16 de Nov. de 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 comentarios
Jan
Jan el 16 de Nov. de 2018
Editada: Jan el 16 de Nov. de 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 el 16 de Nov. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Luna
Luna el 16 de Nov. de 2018
Editada: Luna el 16 de Nov. de 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 comentarios
Jan
Jan el 16 de Nov. de 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 el 16 de Nov. de 2018
Thanks for the explnations and asnwers, this works fine!

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 16 de Nov. de 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 .

Categorías

Más información sobre Install Products en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by