exist() absolute vs relative path

조회 수: 28 (최근 30일)
Timon Viola
Timon Viola 2020년 4월 8일
댓글: Stephen23 2020년 4월 10일
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?
Timon Viola
Timon Viola 2020년 4월 9일
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일
If you're on a newer release, consider using isfile(). https://www.mathworks.com/help/matlab/ref/isfile.html
  댓글 수: 6
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:

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

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by