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 CenterFile Exchange에서 Variables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by