isdir / cd - unexpected results
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I've written the following code, and am shocked that it doesn't work.
Say my working directory is MyFolder and there are other folders and files inside that folder. If I run the following set of commands, the cd function errors because it cannot find 'MyFolder'. The source of the problem is that isdir returns true if the the argument is your current working directory. This isn't a hard problem to solve but I am shocked as a longtime user of Matlab that these lines code create an error. Is this expected behavior?
cdDir = 'MyFolder';
if isdir( cdDir )
cd( cdDir )
end
0 Kommentare
Antworten (2)
Steven Lord
am 20 Sep. 2016
The isdir function uses the exist function. The exist function searches the MATLAB search path to locate the directory whose name you passed into isdir. You can see this if you cd into a directory that has no subdirectory named datafun then ask isdir('datafun'). This will return true because the toolbox/matlab/datafun subdirectory under matlabroot is on the search path.
If you want to use cd to change into a subdirectory of the current directory I'd probably use a try / catch block:
try
cd(new_directory)
catch ME
% It didn't work; handle this appropriately
end
Alternately I'd tell isdir to search for a subdirectory in the current directory using pwd.
subdir = fullfile(pwd, new_directory);
if isdir(subdir)
cd(subdir);
else
% Handle this appropriately
end
0 Kommentare
Stephen23
am 21 Sep. 2016
Bearbeitet: Stephen23
am 21 Sep. 2016
isdir is based on exist, and in reality exist also checks for the existence of the current directory itself, even if this is not on the MATLAB path. The inconsistency arises from treating directories and files differently.
Here is a demonstration of this. Starting from the default directory on Windows (C:\Users\<username>\Documents\MATLAB) without anything added to the path, and adding these nested directories and scripts (within each directory the script and subdirectory have the same number):

Note that all of the subdirectories are grayed out, which indicates that none of them are on the path, but that test1.m, which is in the MATLAB directory, is not grayed out. This is source of the confusion, because exist matches this (sometimes), but dir does not: both test1.m and subdir1 are identified by dir:
>> dir
. .. subdir1 test1.m
And this is the source of the problem: exist checks for both folder within the requested path and also the folder itself. For example, when we change the directory it makes no difference to exist:
>> exist('subdir3','dir') % Current Directory = subdir2
ans =
7
>> exist('subdir3','dir') % Current Directory = subdir3
ans =
7
Remember that none of these directories are on the path, so exist has located that directory as the being within the current directory, and yet will also locate it as being the current directory itself, without making any distinction. This makes it impossible to tell where that folder is! exist does not use the input as a relative path, but follows some other rules...
Now lets try those scripts. All of the test scripts are identical:
disp([...
exist('subdir1','dir'),exist('test1.m','file');...
exist('subdir2','dir'),exist('test2.m','file');...
exist('subdir3','dir'),exist('test3.m','file');...
exist('subdir4','dir'),exist('test4.m','file')])
Lets run them, changing the directory when prompted. This is what is printed in the command window:
>> test1
7 2
0 0
0 0
0 0
This is okay, it agrees with the dir test above.
>> test2
7 2
7 2
0 0
0 0
Also okay. The first row correspond to the MATLAB directory, and so are on the MATLAB path.
>> test3
0 2
7 0
7 2
0 0
Oh! How is subdir2 identified if subdir2 is not on the MATLAB path? Why is subdir1, which is in the MATLAB directory, no longer found? Ditto for the last test script:
>> test4
0 2
0 0
7 0
0 2
Different handling of files and directories causes this problem: dir identifies all folders in the requested directory, whereas exist identifies directories based on the MATLAB path and implicitly the current directory itself, but not those directories in those locations.
Summary: files and folders get treated differently, which is a pain in the proverbial. The solution is simple: use absolute paths. And of course avoid using cd anyway, because it is slow and makes debugging more difficult.
0 Kommentare
Siehe auch
Kategorien
Mehr zu File Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!