exist() absolute vs relative path
Ältere Kommentare anzeigen
I was trying to check for existence of a file defined by absolute path and bumped into a strange behavior.
- If I define the file name with absolute path and without extension and the file is in the current folder exist() finds the file
- If I define the file name with absolute path and without extension and the file is NOT in the current folder exist() does NOT find the file
- If I define the file name with absolute path and with extension and the file is NOT in the current folder exist() does find the file
Is this the expected behavior?
My expectation would be that exist() still finds the file in case 2 OR exist does not find it in case 1 and 2.
2 Kommentare
Although the title of your question is "exist() absolute vs relative path", all of your examples use absolute paths.
I do not see that behavior on R2012b, exist works as expected and documented:
>> F1 = 'C:\Users\stephen.cobeldick\test.txt';
>> F2 = 'C:\Users\stephen.cobeldick\testnoext';
>> fclose(fopen(F1,'wt'));
>> fclose(fopen(F2,'wt'));
>> exist(F1,'file')
ans =
2
>> exist(F2,'file')
ans =
2
As you do not show the exact code that you tried we cannot comment on your attempt. It is possible that you did not specify the second argument, in which case I would not have high expectations of getting a useful output.
What version of MATLAB are you using?
Timon Viola
am 9 Apr. 2020
Antworten (1)
Sean de Wolski
am 8 Apr. 2020
0 Stimmen
If you're on a newer release, consider using isfile(). https://www.mathworks.com/help/matlab/ref/isfile.html
6 Kommentare
Timon Viola
am 9 Apr. 2020
Sean de Wolski
am 9 Apr. 2020
isfile([pwd 'fileName.m']) does not insert a filesep between pwd and 'filename1.m'. Use fullfile; it's you're friend.
isfile(fullfile(pwd, 'fileName.m'))
and
mfilename('fullpath')
Does not include the extension so it's not a file. The files need an extension to be found.
Timon Viola
am 9 Apr. 2020
I would not expect either of these to return true:
'C:\Users\Timon\Documents\matlab\file1.m'
'C:\Users\Timon\Documents\matlab\existTest\testIsFile\file1.m'
because (according to your uploaded zip file and your comment) the actual paths of the files would be:
'C:\Users\Timon\Documents\matlab\existTest\file1.m'
'C:\Users\Timon\Documents\matlab\existTest\folder1\file2.m'
You need to slow down and actually look at what you are doing. For example, you used mfilename to get the full name of a script, which you then used as a folder name in constructing a full filename:
'C:\Users\Timon\Documents\matlab\existTest\testIsFile\file1.m'
% ^^^^^^^^^^ Mfilename!
At the moment there are too many half-baked and incorrect examples (wrong paths, missing file separator, missing file extension, filenames used as folder names, etc.) for us to identify a valid buggy output from exist. I got totally lost in amongst this stream of consciousness thread. Your shotgun approach is bound to hit something, but do you really expect anyone to check and explain every one of your hundred different tests? Please focus! Check your own examples if the path really is valid (i.e. correct path, file separator, etc.) before spamming another fifty lines of code in a new comment.
Note that you are mixing up the behavior of exist with that of isfile: isfile does not know or care about MATLAB registered files, so it expects the complete filename including extension. All of your isfile tests without an extension should fail because you do NOT have any files (in your uploaded zip file) with no extension (which is perfectly valid on both Windows and Linux).
As Sean de Wolski pointed out (and the documentation also states), mfilename('fullpath') does not return an extension, so you would need to add the extension before using it with isfile.
Timon Viola
am 10 Apr. 2020
Stephen23
am 10 Apr. 2020
"This is still case 1 and 2 from my original question."
Yes, I agree that this deserves clarification from TMW. Note that TMW might have an easter break, so it could be worth bumping this thread next week to get attention from Sean de Wolski.
Otherwise you could:
- email them directly (see "Contact Us" at the top of the page), with a link to this thread, or
- create a bug report: https://www.mathworks.com/support/bug_reports/faq.html
Kategorien
Mehr zu File Operations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!