Model callbacks execute at specified action points, for example after you load or save the model.
You can set most of the same callbacks for libraries. Only the callbacks that can execute
for a library are available to set for a library. For example, you cannot set the
InitFcn callback for a library, which is called as part of simulation,
because you cannot simulate a library.
Create Model Callbacks
In the Simulink® Toolstrip, on the Modeling tab, in the Design gallery, click Property Inspector.
With no selection at the top level of your model or referenced model, on the Properties tab, in the Callbacks section, select the callback you want to set.
Block callbacks differ from model callbacks. Do not select a Model block to set model callbacks for a referenced model.
In the box, enter the functions you want the callback to perform.
To programmatically create a model callback, use the
set_param function to assign MATLAB® code to a model callback parameter.
Model Callback Parameters
|Model Callback Parameter||When Executed|
Before the model is loaded.
Do not use model parameters in a
Defining a callback code for this parameter is useful for loading variables that the model uses.
If you want to call your model from a MATLAB file without opening your model, use the
For examples, see:
After the model is loaded.
Defining callback code for this parameter may be useful for generating an interface requiring a loaded model.
Because the Simulink Editor opens after this callback executes, the
During the update phase before block parameters are evaluated. This callback is called during model update and simulation.
For more information on the
For examples, see:
Before the simulation phase. This callback is not called during model update.
After the simulation pauses.
Before the simulation continues.
After the simulation stops.
Output is written to workspace
variables and files before the
Simulation outputs are not available in the
Before the model is saved.
After the model is saved.
If you make structural changes with
Before the block diagram is closed.
Referenced Model Callbacks
In a model hierarchy, the execution of callbacks reflects the order in which the top model and the models it references execute their callbacks. For example, suppose:
References model B in accelerator mode.
PostLoadFcncallback that creates variables in the MATLAB workspace.
Has the Rebuild configuration parameter set to
If changes detected, or
If any changes in known dependencies detected.
CloseFcncallback that clears the MATLAB workspace.
Has not been built or is out of date.
Simulating model A triggers a rebuild of referenced model B. When Simulink rebuilds model B, it opens and closes model B, which invokes the model B
CloseFcn clears the MATLAB workspace, including the variables created by the model A
Instead of using a
CloseFcn callback for model B, you can use a
StopFcn callback in model A to clear the variables used by the model
from the MATLAB workspace. Alternatively, you can use a data dictionary for the data to avoid
the need to have variables in the base workspace.
The simulation mode of a Model block affects when the model callbacks of the referenced model execute.
|Simulation Mode of Model Block||Behavior|
During model compilation, Simulink loads each referenced model that is configured to simulate in normal mode and is not already loaded.
In most cases, Simulink compiles each normal-mode referenced model once, regardless of the number of Model blocks that reference it.
The referenced models remain loaded after model compilation.
During model compilation, Simulink may open and close referenced models that are configured to simulate in accelerator mode based on their Rebuild settings and simulation targets. Models that are open before model compilation remain open.
Simulink does not execute some callbacks.
For more information about model reference simulation modes, see Choose Simulation Modes for Model Hierarchies.