Check for existence of mat-file in search path

조회 수: 38 (최근 30일)
dm
dm 2012년 5월 8일
댓글: Henrik 2016년 7월 6일
Just came over an "interesting" problem. If I run exist('mandrill','file'), I get false back, which I normally would interpret as the (mat)file doesn't exist in the Matlab search path. However, imgdata=load('mandrill') works perfectly fine. Same applies for the 'seamount' image (or any other images that come along with Matlab/toolboxes).
How come Matlab knows where to look for the file/data when using load() and not when using exist()? Problem is that I'd like to have a test similar to
if ~exist('mandrill')
bla bla bla;
return;
else
bla bla bla;
return;
end
But at the moment this won't work. Any suggestions for how to solve this?
If it should matter I use Matlab R2012a.
Best regards, dm
  댓글 수: 1
Henrik
Henrik 2016년 7월 6일
Could be because "exist" is looking for a .m file when you don't specify the fileformat. If you instead write ~exist('mandrill.mat','file') it should work.

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

채택된 답변

dm
dm 2012년 5월 8일
Always too fast to ask. Seems like just adding the extension works fine, e.g. exist('mandrill.mat') returns 2.
Problem solved.
  댓글 수: 1
Jan
Jan 2012년 5월 8일
It is recommended to specify the 2nd parameter for EXIST in every case. Otherwise EXIST could be very slow and the result can be surprising, e.g. if you have a folder of the same name.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by