MATLAB Answers

Model Callbacks in Referenced Models

1 view (last 30 days)
Paul
Paul on 15 Oct 2012
I see inconsistent behavior in the execution of model callback functions in model references. Should the model callback functions of referenced models run whenever the same model callback function runs in the top model? If so, in what order? For example, suppose the top model contains two references to the same model and that model contains two references to different models. Which model callbacks run, how many times, and when? How are model callbacks in referenced models handled during the code generation process?

  0 Comments

Sign in to comment.

Answers (1)

Kaustubha Govind
Kaustubha Govind on 22 Oct 2012
My guess is that the callbacks are executed in the order that each referenced model is compiled - I would expect this to be the same as the block sorted order, but I'm not absolutely certain. Here is a description of when each model callback is executed. A good exercise might be to dump debugging statements in each callback such as:
fprintf('Executing InitFcn callback for %s', gcs)

  2 Comments

Paul
Paul on 23 Oct 2012
The documentation should be clear on this so that we shouldn't have to guess. The documentation for model callbacks doesn't really address this issue other than a Note in the section on Model Callback Functions that suggests that callbacks are in play for referenced models (btw, why did you point to the 2012a documentation instead of 2012b?). The doc on model referencing also doesn't cover model callbacks in referenced models. So I tried an experiment. In all cases, the referenced model has a model callback InitFcn and StopFcn defined. Experiment was run with 2010a, which is all I have handy at the moment.
1. One top model, one reference, all executing in Normal mode: Both callbacks in the referenced model execute.
2. One top model, one reference, executing in Accelerator mode: InitFcn callback executed, StopFcn callback did not execute.
3. One top model, two references, executing in Accelerator mode: InitFcn callback executed once, StopFcn callback did not execute.
If it wasn't for the differenence in behavior between Normal and Acclerator, one could guess that the rule is that Model callbacks in referenced models execute once, regardless of the number of instances. But we shouldn't have to guess. And the difference between Normal/Accelerator is disconerting. Since the documentation doesn't say what should happen (at least I can't find it), it's hard to say if this observed behavior is a bug. Maybe it works differently in later versions?
Do all of the model callback functions even make sense in the context of a referenced model?
Kaustubha Govind
Kaustubha Govind on 23 Oct 2012
Paul: Thanks for posting the results of your experiment.
1) makes sense, because no code is generated for the referenced model, so it is executed normally. 2) makes sense too since InitFcn is executed during model compilation, which happens during code-generation. Then, the generated code is executed, and not the model itself, so the StopFcn will not run (since callbacks are not present in generated code). 3) Could this be because both references are to the same model? Simulink only needs to generate code once.
I agree that this should be clearly documented - could you please submit an enhancement request to MathWorks Tech Support, so that the appropriate team is notified.
I don't recall explicitly looking for R2012a documentation - it may have been what showed up in my web search.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!