MATLAB Answers

exist() absolute vs relative path

17 views (last 30 days)
Timon Viola
Timon Viola on 8 Apr 2020
Commented: Stephen Cobeldick on 10 Apr 2020
I was trying to check for existence of a file defined by absolute path and bumped into a strange behavior.
  1. If I define the file name with absolute path and without extension and the file is in the current folder exist() finds the file
  2. 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
  3. 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 Comments

Stephen Cobeldick
Stephen Cobeldick on 8 Apr 2020
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
Timon Viola on 9 Apr 2020
Hi Stephen,
Thanks for the fast response, I didn't write cases for relative path as I tried to keep it short but see my test runs below.
As I also tagged in the question, I am using R2020a and now I added R2019b (the example was run on R2019b). Unfortunately it is not an option for me to use R2012b.
My general conclusion as of now is that:
exist( ___, 'file') is inconsistent in terms of finding a file. If the file is on the search path MATLAB finds it, if it's not on the search path MATLAB fails. Defining the extension solves this.
The setup looks as:
/existTest/
file1.m
testExist.m (I copied the code below)
testIsFile.m
/existTest/folder2/file2.m
To quote the ref page: If name specifies a file with a non-registered file extension (.mat, .fig, .txt), include the extension. You can also include an extension to prevent conflict with other similar file names. For example, exist file.txt or exist("file.txt"). - So it might be safer to just always define the extension?
I have zipped my setup and attached to this answer if you would prefer looking into that.
%addpath(mfilename('fullpath'),'-end')
echo on
pwd
% execute in /testExist/
exist('file1','file') % find file
exist('file1.m','file') % finds file
exist('folder1/file2','file') % fails
exist('folder1/file2.m','file') % finds file
exist('c:\Users\Timon\Documents\matlab\existTest\file1','file') % finds file but file is on MATLAB path (1.)
exist('c:\Users\Timon\Documents\matlab\existTest\folder1\file2','file') % fail (2.)
exist('c:\Users\Timon\Documents\matlab\existTest\folder1\file2.m','file') % finds file (3.)
cd ..
pwd
% execute 1 level above /testExist/
exist('file1','file') % find file but file is on MATLAB path
exist('file1.m','file') % find file but file is on MATLAB path
exist('existTest/folder1/file2','file') % fails
exist('existTest/folder1/file2.m','file') % find file
exist('c:\Users\Timon\Documents\matlab\existTest\folder1\file2.m','file') % finds file
exist('c:\Users\Timon\Documents\matlab\existTest\folder1\file2','file') % fails
echo off
%%
% MATLAB Version: 9.6.0.1307630 (R2019a) Update 7
% Operating System: Microsoft Windows 10 Pro Version 10.0 (Build 18362)
% Java Version: Java 1.8.0_181-b13 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
% testExist
% pwd
%
% ans =
%
% 'C:\Users\Timon\Documents\matlab\existTest'
%
% % execute in /testExist/
% exist('file1','file') % find file
%
% ans =
%
% 2
%
% exist('file1.m','file') % finds file
%
% ans =
%
% 2
%
% exist('folder1/file2','file') % fails
%
% ans =
%
% 0
%
% exist('folder1/file2.m','file') % finds file
%
% ans =
%
% 2
%
%
% exist('c:\Users\Timon\Documents\matlab\existTest\file1','file') % finds file but file is on MATLAB path (1.)
%
% ans =
%
% 2
%
% exist('c:\Users\Timon\Documents\matlab\existTest\folder1\file2','file') % fail (2.)
%
% ans =
%
% 0
%
% exist('c:\Users\Timon\Documents\matlab\existTest\folder1\file2.m','file') % finds file (3.)
%
% ans =
%
% 2
%
%
% cd ..
% pwd
%
% ans =
%
% 'C:\Users\Timon\Documents\matlab'
%
% % execute 1 level above /testExist/
% exist('file1','file') % find file but file is on MATLAB path
%
% ans =
%
% 2
%
% exist('file1.m','file') % find file but file is on MATLAB path
%
% ans =
%
% 2
%
% exist('existTest/folder1/file2','file') % fails
%
% ans =
%
% 0
%
% exist('existTest/folder1/file2.m','file') % find file
%
% ans =
%
% 2
%
%
% exist('c:\Users\Timon\Documents\matlab\existTest\folder1\file2.m','file') % finds file
%
% ans =
%
% 2
%
% exist('c:\Users\Timon\Documents\matlab\existTest\folder1\file2','file') % fails
%
% ans =
%
% 0
%
% echo off

Sign in to comment.

Answers (1)

Sean de Wolski
Sean de Wolski on 8 Apr 2020
If you're on a newer release, consider using isfile(). https://www.mathworks.com/help/matlab/ref/isfile.html

  6 Comments

Show 3 older comments
Stephen Cobeldick
Stephen Cobeldick on 10 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
Timon Viola on 10 Apr 2020
Thanks for looking into that, I did confuse the paths in that example. isfile is working. I really appreciate your answer.
"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."
So if I focus on the original question regarding exist(), the following one point is still not clear for me:
  • exist finds .m file - defined without extension - if the file is on the path (or in the current folder)
  • exist doesn't find the .m file - defined without extension - if its not on the MATLAB path (or in the current folder). (Probably because it looks for the file without extension and not for the .m file. It is not going to look for MATLAB registered files.)
This is still case 1 and 2 from my original question.
Stephen Cobeldick
Stephen Cobeldick on 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:

Sign in to comment.

Sign in to answer this question.

Products


Release

R2019b