Directory Issue - not finding subfolder

29 ビュー (過去 30 日間)
Kiran Eswaran
Kiran Eswaran 2019 年 8 月 22 日
コメント済み: Kiran Eswaran 2019 年 8 月 23 日
Hi,
(Note: all subfolders and the subfolders' subfolders are on the search path).
I'm currently mapping and extracting data from different folders. I've mapped the present working directory, and its subfolders'. However, when i try to map the subfolders subfolders using dir, i get a message saying that the specific folders are not found. However, if i click the subfolder and attempt to run dir to map the subsubfolder, it works. I need to run this code for a large scale of folders, so clicking each subfolder and running dir by hand is not feasible.
eg NSW -> 2018 -> April 2018 -> file i need to extract.
dir('2018') works, but dir('April 2018') does not work, unless i click into the 2018 folder.
If i change the current directory to 2018 (ie cd 2018), then dir('April 2018') works. However, i need to change the cd back to the parent NSW directory if i use this method, which i haven't been able to figure out how to do.
Any advice on how to approach this issue is appreciated. Apologies if this question has been asked before, but i could not find an answer looking at previous posts.
If i've been unclear, i will clarify any area.
Cheers,
Kiran
  2 件のコメント
Stephen23
Stephen23 2019 年 8 月 22 日
"Note: all subfolders and the subfolders' subfolders are on the search path"
Do NOT do this!
The MATLAB Search Path should only be for code.
In general folders of data should not be on the MATLAB Search Path.
Guillaume
Guillaume 2019 年 8 月 22 日
As Stephen says don't cd or modify matlab search path. It's unnecessary, slow and dangerous.I would recommend that you always use absolute path.
You talk about mapping and it's not clear what you mean by that. One thing that matlab has been known to have trouble with is synchronised folder (e.g. dropbox or onedrive) and networked folder. Is this what you mean by mapping?
For local folder, if it exists, dir will find it as long as you provide the correct path.

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

採用された回答

Stephen23
Stephen23 2019 年 8 月 22 日
編集済み: Stephen23 2019 年 8 月 22 日
"Any advice on how to approach this issue is appreciated."
Either use dir or sprintf to suit your requirements, e.g. (untested, but should get you started):
P = 'path of the root directory'
S = dir(fullfile(P,'*'));
Q = setdiff({S([S.isdir]).name},{'.','..'});
for ii = 1:numel(Q) % loop over subfolders.
S = dir(fullfile(P,Q{ii},'*'));
R = setdiff({S([S.isdir]).name},{'.','..'});
for jj = 1:numel(R) % loop over subsubfolders.
S = dir(fullfile(P,Q{ii},R{jj},'*.txt'));
for kk = 1:numel(S) % loop over files.
F = fullfile(P,Q{ii},R{jj},S(kk).name); % Filename
... process your file
end
end
end
See also:
and many threads that discuss looping over subfolders, e.g.:
etc.
And also:
  3 件のコメント
Walter Roberson
Walter Roberson 2019 年 8 月 23 日
Mathworks puts a fair number data folders on the search path. That is the reason why you can
imshow('cameraman.tif')
without having to use an auxiliary function to locate the file or directory.
Kiran Eswaran
Kiran Eswaran 2019 年 8 月 23 日
Adapted it slightly and works an absolute charm, you are a legend Stephen, very clear code and learnt along the way, really appreciate your help mate!

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2019 年 8 月 22 日
Since you're using release R2016b, you can use the functionality added to dir in that release to recursively search through folders and subfolders. Let me find all MAT-files in subdirectories of the matlab directory in the toolbox directory under matlabroot.
>> cd(matlabroot)
>> cd toolbox/matlab
>> D = dir('**/*.mat');
Because I'm using release R2019a (what I have open right now) the specific numbers may be different from the results you receive in your installation of release R2016b, but for my installation item 43 in the struct returned by dir is one of the MAT-files used by some of the MATLAB demos.
>> D(43)
ans =
struct with fields:
name: 'usborder.mat'
folder: 'C:\Program Files\MATLAB\R2019a\toolbox\matlab\demos'
date: '14-Mar-2004 10:32:22'
bytes: 1735
isdir: 0
datenum: 7.3202e+05
If I want to work with it, I can build the full file name and pass that into functions like whos, load, etc.
>> theFullName = fullfile(D(43).folder, D(43).name);
>> whos('-file', theFullName)
>> data = load(theFullName);
No changing directory required except in the setup before I called dir, and even that I could have avoided using fullfile:
>> Q = fullfile(matlabroot, 'toolbox', 'matlab', '**/*.mat');
>> D2 = dir(Q);
>> isequal(D, D2) % true
  1 件のコメント
Kiran Eswaran
Kiran Eswaran 2019 年 8 月 23 日
Awesome, cheers for your prompt response, i will try this!!

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

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by