'exist' function returning false positive for directory.
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I'm trying to check if a file/directory path is valid before using it. I've seen countless examples of the 'exist' command to check to see if the argument is real, but I've found that MATLAB lies to you if you if the directory is somewhere on the path, but not accessible.
Consider the following example.
cd C:/
mkdir a; cd a
mkdir b; cd b
mkdir c; cd c
% Now current directory is C:/a/b/c
% I'm given an input dir called "b/c"
exist('b/c','dir') % Returns 7 - Indicating that "directory does exist"
% Thinking that's a real directory, I attempt to access it and get an error.
cd b/c
%:: Error using cd
%:: Cannot CD to C:\a\b\c\b\c (Name is nonexistent or not a directory).
What alternatives to 'exist' are there to correctly identify the existence of a folder or file if and only if:
- the absolute path, relative to '/' or 'C:', exists, or
- the relative path, from the current directory, exists?
I could try shelling out to cmd, but that seems hacky:
system('IF EXIST b/c (exit /B 1) ELSE (exit /B 0)')
Any suggestions?
0 Kommentare
Akzeptierte Antwort
Jan
am 1 Feb. 2019
Bearbeitet: Jan
am 1 Feb. 2019
Since R2017b the command isfolder is available and safer than exist('dir').
A problem of your approach is the usage of relative path names. Remember that a GUI or TIMER callback can change the current directory unexpectedly. In parallel programming this is a common problem also. So the solution is easy: Never work with relative paths.
A clean version:
base = 'C:\';
mkdir(fullfile(base, 'a', 'b', 'c');
want = 'C:/a/b/c';
isfolder(fullfile(want, 'b\c')) % FALSE
isfolder(fullfile(base, 'b\c')) % FALSE
Note, that any crazy callback cannot cofuse this by changing the current directory.
If do not understand, why exist('b/c','dir') returns 7, if the current folder is C:\a\b\c . Maybe b\c is existing anywhere in Matlab's path ? Remember that exist checks the complete path.
By the way, why do you assume, that
mkdir a; cd a
creates 'C:\a' and not the directory 'C:\a; cd a'
? It works as you expect it, but I do not know, where this is documented.
1 Kommentar
Brahm Powell
am 15 Apr. 2020
Bearbeitet: Brahm Powell
am 15 Apr. 2020
Semicolons can be used to separate two commands in "one line" of code. This is heavily documented. Semicolons are used to separate commands (or rows within an array). Mkdir and cd are no different. In fact, an example of this usage can be found at https://www.mathworks.com/help/matlab/ref/pathsep.html.
And I would hesitate to make such a bold statement as "Never work with relative paths". There are many cases where relative path manipulation is necessary or preferred. For example, if your script needs to be able to be run and work exactly the same way in multiple different working directories (by manipulating the files around it, relative to its current location), then relative paths would certainly be prefferred. Or, if you are working on a group project (as many people do), you would certainly want your team to be able to drag-and-drop your code to wherever they want on their host system, and not have to worry about the fact that their system paths and stored files may be completely different (not to mention, they might not even have the same OS!). In such a case, you would certainly want paths that are determined relative to the project directory, and not the absolute path.
Now, for the question asked by the OP, I would not object to the use of absolute paths (he specifically wanted to know how to check if both the absolute and relative paths exist). However, generally speaking (for those who find this post via a google search), the use of relative or absolute paths should be evaluated on a case-by-case basis.
Weitere Antworten (0)
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!