Where does "load" look for files?
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
J. M. Groh
am 17 Nov. 2020
Bearbeitet: Walter Roberson
am 17 Nov. 2020
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.
0 Kommentare
Akzeptierte Antwort
Ameer Hamza
am 17 Nov. 2020
What about using this syntax.
load('./myfile.mat')
4 Kommentare
Stephen23
am 17 Nov. 2020
or using the recommended fullfile:
load(fullfile('.','myfile.mat'))
Weitere Antworten (1)
Walter Roberson
am 17 Nov. 2020
Bearbeitet: Walter Roberson
am 17 Nov. 2020
"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.
Siehe auch
Kategorien
Mehr zu Filename Construction finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!