exist() absolute vs relative path

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

Stephen23
Stephen23 2020년 4월 8일
편집: Stephen23 2020년 4월 8일
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?
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

댓글을 달려면 로그인하십시오.

답변 (1개)

Sean de Wolski
Sean de Wolski 2020년 4월 8일

0 개 추천

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

댓글 수: 6

Hi,
isfile() works brilliantly for relative paths. And fails every single call when I use absolute paths. Even fails for
isfile([pwd 'fileName.m'])
and
isfile(mfilename('fullpath'))
Please see my other attempts below.
From the ref page: File name, specified as a string array, character vector, or cell array of character vectors. For a local file, fileName can include a relative path, but the relative path must be in the current folder. Otherwise, fileName must include a full path. If the file is stored at a remote location, then fileName must contain a full path specified as a uniform resource locator (URL). For more information, see Work with Remote Data.
Otherwise, fileName must include a full path. - I tried this.
Am I missing/overlooking something?
Thank you in advance for the clarification.
echo on
pwd
% execute in /testExist/
isfile('file1') % fails
isfile('file1.m') % finds file
isfile('folder1/file2') % fails
isfile('folder1/file2.m') % finds file
pwd
isfile([pwd 'file1.m']) % fails
mfilename('fullpath')
isfile(mfilename('fullpath')) % fails
isfile('c:\Users\Timon\Documents\matlab\isfileTest\folder1\file2') % fails
isfile('c:\Users\Timon\Documents\matlab\isfileTest\folder1\file2.m') % fails
cd ..
pwd
% execute 1 level above /testExist/
isfile('existTest/file1') % fails
isfile('existTest/file1.m') % finds file
isfile('existTest/folder1/file2') % fails
isfile('existTest/folder1/file2.m') % finds file
isfile('c:\Users\Timon\Documents\matlab\isfileTest\file1') % fails
isfile('c:\Users\Timon\Documents\matlab\isfileTest\file1.m') % fails
isfile('c:/Users/Timon/Documents/matlab/isfileTest/file1.m') % fails
isfile('c:\Users\Timon\Documents\matlab\isfileTest\folder1\file2.m') % fails
isfile('c:\Users\Timon\Documents\matlab\isfileTest\folder1\file2') % 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
testIsFile
pwd
ans =
'C:\Users\Timon\Documents\matlab\existTest'
% execute in /testExist/
isfile('file1') % fails
ans =
logical
0
isfile('file1.m') % finds file
ans =
logical
1
isfile('folder1/file2') % fails
ans =
logical
0
isfile('folder1/file2.m') % finds file
ans =
logical
1
pwd
ans =
'C:\Users\Timon\Documents\matlab\existTest'
isfile([pwd 'file1.m'])
ans =
logical
0
mfilename('fullpath')
ans =
'C:\Users\Timon\Documents\matlab\existTest\testIsFile'
isfile(mfilename('fullpath'))
ans =
logical
0
isfile('c:\Users\Timon\Documents\matlab\isfileTest\folder1\file2') % fails
ans =
logical
0
isfile('c:\Users\Timon\Documents\matlab\isfileTest\folder1\file2.m') % fails
ans =
logical
0
cd ..
pwd
ans =
'C:\Users\Timon\Documents\matlab'
% execute 1 level above /testExist/
isfile('existTest/file1') % fails
ans =
logical
0
isfile('existTest/file1.m') % finds file
ans =
logical
1
isfile('existTest/folder1/file2') % fails
ans =
logical
0
isfile('existTest/folder1/file2.m') % finds file
ans =
logical
1
isfile('c:\Users\Timon\Documents\matlab\isfileTest\file1') % fails
ans =
logical
0
isfile('c:\Users\Timon\Documents\matlab\isfileTest\file1.m') % fails
ans =
logical
0
isfile('c:/Users/Timon/Documents/matlab/isfileTest/file1.m') % fails
ans =
logical
0
isfile('c:\Users\Timon\Documents\matlab\isfileTest\folder1\file2.m') % fails
ans =
logical
0
isfile('c:\Users\Timon\Documents\matlab\isfileTest\folder1\file2') % fails
ans =
logical
0
echo off
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.
Hi
This still doesn't work on R2019b. Just like in other cases, when fullpath was defined.
[pwd filesep 'file1.m']
ans =
'C:\Users\Timon\Documents\matlab\file1.m'
isfile([pwd filesep 'file1.m'])
ans =
logical
0
[mfilename('fullpath') filesep 'file1.m']
ans =
'C:\Users\Timon\Documents\matlab\existTest\testIsFile\file1.m'
isfile([mfilename('fullpath') filesep 'file1.m'])
ans =
logical
0
Stephen23
Stephen23 2020년 4월 10일
편집: Stephen23 2020년 4월 10일
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 2020년 4월 10일
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.
Stephen23
Stephen23 2020년 4월 10일
"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:

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

제품

릴리스

R2019b

질문:

2020년 4월 8일

댓글:

2020년 4월 10일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by