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

11 ビュー (過去 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 Center および File ExchangeDeployment, 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