Where does "load" look for files?

조회 수: 11 (최근 30일)
J. M. Groh
J. M. Groh 2020년 11월 17일
편집: Walter Roberson 2020년 11월 17일
Recently I have observed that load('myfile.mat') seems to find and load "myfile.mat" even if it does not exist in the current folder but does exist elsewhere on the matlab path. I don't see this behavior documented in the help for the load function. Is there an argument to the function call that would force it to only look locally to the current folder?
Specifically, I'd like it to behave like this:
curfolder=pwd; full_file_name=fullfile(curfolder,'myfile.mat'); load(full_file_name);
But this workaround reduces code readability.
I'm also curious to know if this is a recent change to the load function or if it has always been this way. I'm running R2016b at the moment.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 11월 17일
What about using this syntax.
load('./myfile.mat')
  댓글 수: 4
Stephen23
Stephen23 2020년 11월 17일
or using the recommended fullfile:
load(fullfile('.','myfile.mat'))
J. M. Groh
J. M. Groh 2020년 11월 17일
yeah that's a bit more readable

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2020년 11월 17일
편집: Walter Roberson 2020년 11월 17일
"Where Does MATLAB Look for Files?
When you do not specify a path to a file, MATLAB® looks for the file in the current folder or on the search path."
You could change your MATLAB path to be empty so that it would only look in the current folder.
Or you could use your own load function that checks to see if the file exists in the current directory before calling the matlab load(), and then take care to assignin('caller') for each of the variables since you are using the not-recommended syntax of not providing an output location.
  댓글 수: 1
J. M. Groh
J. M. Groh 2020년 11월 17일
Thanks for pointing to that documentation. I had previously interpreted the search path as the places where matlab looks for code files (e.g. *.m ) rather than data files (*.mat or other formats). Guess that was not a correct interpretation.

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

카테고리

Help CenterFile Exchange에서 Filename Construction에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by