Simulink Model Callbacks - How can I tell if the user pressed Run or Build?
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Venkatachala Sarma
el 10 de Mzo. de 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.
0 comentarios
Luke Johnston
el 20 de Jun. de 2020
Editada: Luke Johnston
el 20 de Jun. de 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 comentario
Luke Johnston
el 5 de En. de 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.
Ver también
Categorías
Más información sobre Deployment, Integration, and Supported Hardware en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!