Model Callbacks
Model callbacks execute at specified action points, for example, after you load or save the model.
You can use model callbacks to perform common tasks, such as Automatically Initialize Variables and Load Data.
Libraries and subsystem references support most of the model callbacks. However, for these
components, you can set only those callbacks that can execute for a library or subsystem
reference. For example, you cannot set the InitFcn
callback for a library,
which is called as a part of simulation, because you cannot simulate a library. This same
behavior applies to subsystem references.
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.
Note
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 an example, see Automatically Initialize Variables and Load Data. Limitations include:
|
| After the model is loaded. Defining callback code for this parameter may be useful for generating an interface requiring a loaded model. Limitations include:
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. Avoid
Avoid For more information on the
For an example, see Call MATLAB Function Files in MATLAB Function Blocks. |
| Before the simulation phase. This callback is not called during model update. This callback is called for every simulation in fast restart. |
| 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
This callback is called for every simulation in fast restart. |
| Before the model is saved. |
| After the model is saved. If you make structural changes with
|
| Before the block diagram is closed. Any
|
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:
Model A:
References model B in accelerator mode.
Has a
PostLoadFcn
callback that creates variables in the MATLAB workspace.Has the Rebuild configuration parameter set to
Always
,If changes detected
, orIf changes in known dependencies detected
.
Model B:
Has a
CloseFcn
callback 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
callback. CloseFcn
clears the MATLAB workspace, including the variables created by the model A
OpenFcn
callback.
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 |
---|---|
Normal | 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
The referenced models remain loaded after model compilation. |
Accelerator | 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.