Can I use fullfile() to read two subfolders ?

8 ビュー (過去 30 日間)
Emma
Emma 2018 年 11 月 16 日
コメント済み: Emma 2018 年 11 月 16 日
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
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
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
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
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.
Emma
Emma 2018 年 11 月 16 日
Thanks for the explnations and asnwers, this works fine!

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
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 .

カテゴリ

Help Center および File ExchangeInstall Products についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by