How to directly load data to workspace from main function

10 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Variables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by