Why use x=load('myFile.mat')?

16 views (last 30 days)
K E
K E on 10 Feb 2012
Edited: Stephen23 on 20 Aug 2019
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

Jiro Doke
Jiro Doke on 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
  1 Comment
K E
K E on 22 Feb 2012
Thanks so much for this clarification.

Sign in to comment.

More Answers (2)

Stephen23
Stephen23 on 21 Aug 2017
Edited: Stephen23 on 20 Aug 2019
From the discussion following this answer:
"why should people never ever load mat files into the workspace?"
Because load-ing into a structure is an easy way to:
  1. avoid pointless bugs because variable tracking is simpler.
  2. make input checking simpler, if the user want to check the input values.
  3. allow either an arbitrary or fixed number of parameters to be processed. This is very useful for optional arguments for any process.
  4. allow the static code checking tools to work effectively.
  5. prevent accidental overwriting of existing variables.
  6. allow the MATLAB JIT code optimization to work efficiently with the loaded data.
  7. allow the MATLAB parser to uniquely identify variables and functions.
  8. make it trivially easy to efficiently process all fields (e.g. loop over numbered fields).
  9. write clear code that is easy to understand, which reduces bugs.
In comparison using load directly into the workspace:
  1. has the needless risk of accidentally overwriting existing variables.
  2. offers no efficient way to check which variables have been loaded.
  3. hinders the JIT code optimization.
  4. is ambiguous for the parser which object is being referenced (e.g. example).
  5. makes debugging harder as the origin of variables cannot be traced.
  6. ...which also means that the static code checking tools are not effective.
  7. 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.:
"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
Eric on 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 Comments
K E
K E on 17 Feb 2012
If the problem is documentation, I would be OK if I used the following, right?
load('myFile.mat', 'myVariable')
myVariable is often a structure and this lets me avoid the 'structure of structures' that results from
newStructure = load('myFile.mat', 'oldStructure') ;
Jan
Jan on 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.

Sign in to comment.

Categories

Find more on Get Started with MATLAB 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!