Directory Issue - not finding subfolder
28 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Kiran Eswaran
el 22 de Ag. de 2019
Comentada: Kiran Eswaran
el 23 de Ag. de 2019
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 comentarios
Stephen23
el 22 de Ag. de 2019
"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
el 22 de Ag. de 2019
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.
Respuesta aceptada
Stephen23
el 22 de Ag. de 2019
Editada: Stephen23
el 22 de Ag. de 2019
"Any advice on how to approach this issue is appreciated."
- Do not use cd.
- Do not add data folders to the Search Path.
- Use absolute or relative file paths.
- Construct the file paths using fullfile.
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.:
https://www.mathworks.com/matlabcentral/answers/437494-how-to-loop-through-all-files-in-subfolders-in-a-main-folder
https://www.mathworks.com/matlabcentral/answers/293919-how-to-loop-through-subfolders-and-apply-a-particular-function#answer_375509
https://www.mathworks.com/matlabcentral/answers/359493-cannot-iterate-over-subdirectories-from-data-structure
etc.
And also:
3 comentarios
Walter Roberson
el 23 de Ag. de 2019
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.
Más respuestas (1)
Steven Lord
el 22 de Ag. de 2019
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
Ver también
Categorías
Más información sobre File Operations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!