Why use x=load('myFile.mat')?
Show older comments
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.
Accepted Answer
More Answers (2)
"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"
Eric
on 17 Feb 2012
0 votes
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
Categories
Find more on Variables in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!