Simulink Model Callbacks - How can I tell if the user pressed Run or Build?
10 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(2 个)
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.
0 个评论
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
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 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!