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

9 views (last 30 days)
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?

Answers (2)

Venkatachala Sarma
Venkatachala Sarma on 10 Mar 2016
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 on 20 Jun 2020
Edited: Luke Johnston on 20 Jun 2020
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 Comment
Luke Johnston
Luke Johnston on 5 Jan 2021
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.

Sign in to comment.

Categories

Find more on Deployment, Integration, and Supported Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!