フィルターのクリア

How to directly load data to workspace from main function

9 ビュー (過去 30 日間)
Erik
Erik 2011 年 9 月 19 日
Hi!
My problem is that I want to use a main function to first load data from a mat file before running a simulink model. the main function isn't returning any value and when I try to load the mat file no values are stored in the global workspace. If I load directly from the command window it works fine but the idea is to only run the main function.
My next question is similar but instead I would like to run a m-function from the main function and store the received data directly in the workspace.
Thank you in advance!!

回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2011 年 9 月 19 日
When you run load() in Command Window or through a script, the data is loaded into the 'base' workspace. When you run load() inside a function, the data is loaded into the workspace of that function.
If you Simulink model requires just loading those data but not much processing, you can set up the 'PreLoadFcn' callback of the Simulink model to load the data. If the data needs to be processed before being used by the model, I suggest you do those processing in a script, not in a function.
There are ways to assign data into the 'base' workspace inside a function, such as evalin() or assignin(), but it is not recommended.
If for whatever reason, you have to run that main function, I suggest you adding return arguments to the main function so you can pass data to the 'base' workspace. But of course, if there are many variables, the approach is not very practical.
  2 件のコメント
TAB
TAB 2011 年 9 月 19 日
I have read so many times that functions evalin() and eval() are not recommended. But don't know the reason.
Jiang, please can you help me?
Fangjun Jiang
Fangjun Jiang 2011 年 9 月 19 日
Here is a link. http://www.mathworks.com/support/tech-notes/1100/1103.html

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

カテゴリ

Help Center および File ExchangeModel, Block, and Port Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by