How to directly load data to workspace from main function

23 views (last 30 days)
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!!

Answers (1)

Fangjun Jiang
Fangjun Jiang on 19 Sep 2011
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 Comments
TAB
TAB on 19 Sep 2011
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 on 19 Sep 2011
Here is a link. http://www.mathworks.com/support/tech-notes/1100/1103.html

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!