Load .mat - file to base workspace?

134 ビュー (過去 30 日間)
Joakim Magnusson
Joakim Magnusson 2015 年 6 月 25 日
コメント済み: Stephen23 2017 年 10 月 26 日
I'm writing a gui script. I want to get a .mat - file and i need to use it in many functions in my script. Is loading the file to the base workspace a good solution and in that case, how do i load it to the base workspace? "load(filename)" only loads the file to the callers workspace if I'm right.
  2 件のコメント
Joakim Magnusson
Joakim Magnusson 2015 年 6 月 25 日
I've manage to do it by using load and then assign, but this doesn't feel like a good solution.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2015 年 6 月 25 日
編集済み: Stephen23 2015 年 6 月 29 日
Although beginners love making variables pop into existence in different workspaces, this is not robust programming, it is slow, and it removes lots of useful code checking tools. Yes, there are lots of code checking tools that simply do not work when you use eval, evalin or assignin. Click on a variable name in the editor, and it highlights all instances of that variable: does not work. Find and replace: does not work. Editor warnings: do not work. Because these variables just magically appear without warning, MATLAB cannot help you write your code, as it can when variables are passed and created correctly.
The typical functions used for this sloppy programming are eval, evalin, assignin. These are tools are the equivalent of a chainsaw, and should not be used where only a letter-opener is required. Their usage is a topic that has been covered many times before on this forum, and the usual consensus is "do not use these tools for basic assignment or moving variables around". How about these quotes from the MATLAB documentation "Share Data Between Workspaces":
" Best Practice: Passing Arguments The most secure way to extend the scope of a function variable is to use function input and output arguments"
And the least recommended method given on that page is:
"The evalin and assignin functions allow you to evaluate commands or variable names from strings and specify whether to use the current or base workspace. Like global variables, these functions carry risks of overwriting existing data. Use them sparingly."
Using them sparingly ideally means only using them in situations where none of the other methods will work. And, if a program is written well, this will be almost never...
To know more you can search this forum for "dynamic variable names" or "eval", and also read this page (while it is about eval, the same basic issues arise with evalin, etc):
And from MATLAB themselves:
  2 件のコメント
J Eduardo Mucino
J Eduardo Mucino 2017 年 10 月 25 日
編集済み: J Eduardo Mucino 2017 年 10 月 25 日
While the above points are valid, it is an unfortunate reality that MATLAB sometimes forces you to do things you would rather not do. For example, bus objects and referenced configuration sets must be defined in the base workspace in order for Simulink to find them. Therefore, if you are calling sim() on a Simulink model from inside a function (as you might do if you want to automate certain things), you are forced to eval things into the base workspace by design.

サインインしてコメントする。

その他の回答 (1 件)

Anthony Poulin
Anthony Poulin 2015 年 6 月 25 日
Hello, you might use the evalin function:
evalin('base', 'load(''matlab.mat'')');

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by