Why use x=load('myFile.mat')?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Why should I use
newStructure = load('myFile.mat') ;
instead of
load('myFile.mat') ;
I think it has something to do with memory, but please enlighten me.
0 Kommentare
Akzeptierte Antwort
Jiro Doke
am 17 Feb. 2012
You may actually get an incorrect answer in certain situations.
For example, create a MAT file, myFile.mat, that has a variable named myfun = [1 2 3 4]. Now create the following function:
function out = myfun(in)
out = sqrt(in);
end
Then create your test function:
function out = example()
load('myFile.mat');
out = myfun(2);
end
Your intent above is to index into the 2nd element of variable myfun. But when you run this, MATLAB actually calls the function myfun, because at runtime MATLAB can't parse the code and know that the MAT file contains a variable called myfun.
Calling load with an output informs MATLAB that there will be a known variable in the workspace:
function out = example()
d = load('myFile.mat');
out = d.myfun(2);
end
I believe this would work as well:
function out = example()
load('myFile.mat', 'myfun');
out = myfun(2);
end
Weitere Antworten (2)
Stephen23
am 21 Aug. 2017
Bearbeitet: Stephen23
am 20 Aug. 2019
"why should people never ever load mat files into the workspace?"
Because load-ing into a structure is an easy way to:
- avoid pointless bugs because variable tracking is simpler.
- make input checking simpler, if the user want to check the input values.
- allow either an arbitrary or fixed number of parameters to be processed. This is very useful for optional arguments for any process.
- allow the static code checking tools to work effectively.
- prevent accidental overwriting of existing variables.
- allow the MATLAB JIT code optimization to work efficiently with the loaded data.
- allow the MATLAB parser to uniquely identify variables and functions.
- make it trivially easy to efficiently process all fields (e.g. loop over numbered fields).
- write clear code that is easy to understand, which reduces bugs.
In comparison using load directly into the workspace:
- has the needless risk of accidentally overwriting existing variables.
- offers no efficient way to check which variables have been loaded.
- hinders the JIT code optimization.
- is ambiguous for the parser which object is being referenced (e.g. example).
- makes debugging harder as the origin of variables cannot be traced.
- ...which also means that the static code checking tools are not effective.
- allows numbered variables to be loaded which then cannot be accessed efficiently without writing slow, buggy, obfuscated code.
"So you suggest every one should be always loading mat files into structures, then reading from the structures."
Yes. Experienced MATLAB users write code that load-s into a structure, because it is much more efficient, easier to debug, and much more reliable. Experienced MATLAB users give this advice to beginners because some beginners are interested in learning how to write efficient and reliable code, e.g.:
This is also the advice that TMW staff give, e.g.:
https://www.mathworks.com/matlabcentral/answers/335253-mat-files-not-loading-correctly#answer_262994
"and command who is not exactly the same as command whos"
Both whos and who are run-time introspection, which as the MATLAB documentation states, is not efficient: "Run-time introspection is computationally expensive"
0 Kommentare
Eric
am 17 Feb. 2012
I think it has more to do with code readability. If you use the latter form you create variables in the workspace in a manner in which it's not clear from whence they came. In the former it's clear that the variables were loaded from the MAT file.
I use both at times. If I use the latter form I generally add comments indicating what variables are being added to the workspace.
-Eric
2 Kommentare
Jan
am 21 Aug. 2017
Prefer:
FileData = load('myFile.mat', 'oldStruct');
newStruct = FileData.oldStruct;
to keep your workspace clean and support the JIT acceleration of the code.
Siehe auch
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!