exist() absolute vs relative path
33 次查看(过去 30 天)
显示 更早的评论
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 个评论
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?
回答(1 个)
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 个评论
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:
- 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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!