Can I use fullfile() to read two subfolders ?
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
Jan
2018-11-16
编辑:Jan
2018-11-16
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
2018-11-16
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.
采纳的回答
Luna
2018-11-16
编辑:Luna
2018-11-16
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 个评论
Jan
2018-11-16
+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.
更多回答(1 个)
Walter Roberson
2018-11-16
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 .
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Install Products 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!