Simulink Model Callbacks - How can I tell if the user pressed Run or Build?

조회 수: 7 (최근 30일)
Robert
Robert 2016년 2월 23일
댓글: Luke Johnston 2021년 1월 5일
How can my model's InitFcn tell the difference between initializing after the user pressed Run versus after the user pressed Build (for generating code)?
Some context: I generate C code from my model using Simulink Coder and Embedded Coder. When I play data through the model, I use the first and last time value in the data as my start and stop times. When I build the model, Simulink produces an error if the start time is not set to zero (I don't understand why that is important). I would like to add to my InitFcn a check that warns me if the start time is not zero when I am generating code, but not when I am running the model. How can I tell the difference from within the InitFcn?

답변 (2개)

Venkatachala Sarma
Venkatachala Sarma 2016년 3월 10일
It is currently not possible to detect whether the user hit the run button or build button in any callback. As a workaround, you could try to create wrappers in the command line. For simulation use the 'sim' command and for code generation use rtwbuild. As an other way, you could try to create subsystem blocks with callback in 'openfcn' as done in the example model 'rtwdemo_atomic'.
Hope this helps.

Luke Johnston
Luke Johnston 2020년 6월 20일
편집: Luke Johnston 2020년 6월 20일
I've not tested this too much, but what works for my use case is to take a look at the RTWGenSettings model parameter. When run from the model InitFcn callback it appears it is populated during code generation and empty during simulation. Tested in r2019a.
Example:
% Use RTWGenSettings as a way to tell if model is building or simulating
RTWGenSettings = get_param(model_name,'RTWGenSettings');
if isempty(RTWGenSettings)
assignin('base','SIMULATION_FLAG',1);
else
assignin('base','SIMULATION_FLAG',0);
end
  댓글 수: 1
Luke Johnston
Luke Johnston 2021년 1월 5일
Follow-up - I thought this was pretty promising but after some more usage I've found it hasn't been very reliable.
I've switched to leaving a breadcrumb in the base workspace from the make_rtw_hook (in my case ert_make_rtw_hook.m) entry callback that I then look for in the model's InitFcn.
Example:
in ert_make_rtw_hook.m
case 'entry'
% Leave breadcrumb that we passed through ERT hooks
evalin('base', 'breadcrumb = 1');
in your model's InitFcn callback
% Use breadcrumb from ert_make_rtw_hook.m to determine if we're codegen or simulating
if evalin('base', 'exist(''breadcrumb'');')
evalin('base', 'clear(''breadcrumb'');');
assignin('base', 'simulating', 0);
else
assignin('base', 'simulating', 1);
end
In the above, simulating can be used by your model to have different behaviors whether you are in simulation or in code generation using variant blocks. If you just care about doing different things in InitFcn, then replace the assignin with whatever code you want to run.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Deployment, Integration, and Supported Hardware에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by