MATLAB Answers

Running Simulink from within a Matlab function

30 ビュー (過去 30 日間)
Enrico Anderlini
Enrico Anderlini 2017 年 3 月 9 日
コメント済み: Enrico Anderlini 2017 年 3 月 15 日
I was running Simulink from the main script with FastRestart and avoiding the use of simset and all was good.
However, after enclosing the script in a function so that I can call it from a different script for different input data, I now get an error related to the workspace. Basically, Simulink is no longer able to access the workspace within the function.
I have found solutions on-line using simset, but this is not an option in my case. Assuming sfile is my Simulink file, I first load the system
load_system(sfile);
and then set-up the FastRestart settings
set_param(sfile,'FastRestart','on');
set_param(sfile,'SaveFinalState','on');
set_param(sfile,'SaveCompleteFinalSimState','on');
set_param(sfile,'SimulationCommand','update');
Now, the error occurs on this last line, i.e. when I am updating the Simulink file. Basically, Simulink returns an error because it does not recognize mdl.tStep, i.e. the step size length, as a structure because it is not specified. In fact, it is because it is in the workspace.
So, my question is how I can sort out this problem. Is it possible to use set_param to change the workspace now that simset is deprecated? Or should I rather set all variables through set_param individually?

  0 件のコメント

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

回答 (1 件)

Brandon Eidson
Brandon Eidson 2017 年 3 月 15 日
Hey Enrico, the reason you are getting an error now that you have enclosed the script in a function is because a function has its own workspace separate from the base workspace. You can read more about this distinction in the documentation linked to below.
Because you call the model from the function, the model only has access to the function workspace. If the simulation depends on any user-defined variables, such as your "tStep", and you want to update or simulation the model from a function, you will need to pass those user-defined variables to the function as arguments.

  1 件のコメント

Enrico Anderlini
Enrico Anderlini 2017 年 3 月 15 日
Thanks Brandon. You are absolutely right. I sorted out the problem by assigning all variables with the assignin command. Not a very neat solution, but effective nonetheless.

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

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by