Main Content

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

  1. In the Simulink® Toolstrip, on the Modeling tab, in the Design gallery, click Property Inspector.

  2. 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.

  3. 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 ParameterWhen Callback Executes

PreLoadFcn

Before the model is loaded.

Do not use model parameters in a PreLoadFcn model callback because parameters are loaded after the model is loaded. Instead, use a PostLoadFcn callback to work with model parameters when the model is loaded.

Defining callback code for this parameter is useful for loading variables that the model uses.

To call your model from a MATLAB script or function without opening your model, use the load_system function so that the PreLoadFcn callback executes.

For an example, see Automatically Initialize Variables and Load Data.

Limitations include:

  • For the PreLoadFcn callback, the get_param function does not return the model parameter values because the model is not yet loaded. Instead, the get_param function returns:

    • The default value for a standard model parameter, such as solver

    • An error message for a model parameter added with add_param

  • Programmatic access to scopes is not supported.

PostLoadFcn

After the model is loaded.

Defining callback code for this parameter can be useful for generating an interface that requires a loaded model.

Limitations include:

  • If you make structural changes with the PostLoadFcn callback, the callback does not set the Dirty flag of the model to indicate unsaved changes. When you close the model, the software does not prompt you to save.

  • Programmatic access to scopes is not supported.

Because the Simulink Editor opens after this callback executes, the PostLoadFcn callback is not suitable for setting up the model view, for example, setting a zoom factor. Save zoom information with the model to open it with a particular zoom factor.

InitFcn

During the update phase before block parameters are evaluated. This callback is called during model update and simulation.

Avoid InitFcn model callbacks that edit the structure of the model. The software can execute or ignore these callbacks based on the model load status.

Avoid InitFcn model callbacks that edit the contents of a referenced model. Doing so can result in unexpected behavior such as errors or incorrect results when you simulate a Simulink model.

For more information on the InitFcn callback, see Initialization Function.

For an example, see Call MATLAB Function Files in MATLAB Function Blocks.

StartFcn

Before the simulation phase. This callback is not called during model update.

This callback is called for every simulation in fast restart.

PauseFcn

After the simulation pauses.

ContinueFcn

Before the simulation continues.

StopFcn

After the simulation stops.

Output is written to workspace variables and files before the StopFcn callback is executed.

Simulation outputs are not available in the StopFcn callbacks for command-line simulations.

This callback is called for every simulation in fast restart.

PreSaveFcn

Before the model is saved.

PostSaveFcn

After the model is saved.

If you make structural changes with the PostSaveFcn callback, the callback does not set the Dirty flag of the model to indicate unsaved changes. When you close the model, the software does not prompt you to save.

CloseFcn

Before the block diagram is closed.

ModelCloseFcn and DeleteFcn block callbacks are called before the CloseFcn model callback.

DestroyFcn block callbacks are called after the CloseFcn model callback.

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, or If 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 the software rebuilds model B, it opens and closes model B, which invokes the CloseFcn callback of model B. The CloseFcn callback clears the MATLAB workspace, including the variables created by the OpenFcn callback of 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 BlockBehavior
Normal

During model compilation, the software loads each referenced model that is configured to simulate in normal mode and is not already loaded.

In most cases, the software compiles each normal-mode referenced model once, regardless of the number of Model blocks that reference it.

The InitFcn, StartFcn, PauseFcn, ContinueFcn, and StopFcn model callbacks execute for each executable form of the model created by the model compiler.

The referenced models remain loaded after model compilation.

Accelerator

During model compilation, the software can 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.

The software does not execute some callbacks.

  • If everything is up to date and the Rebuild configuration parameter is set to If changes in known dependencies detected, then the referenced model does not compile and its InitFcn callbacks do not execute.

  • Callbacks such as StartFcn and StopFcn do not execute because referenced models in accelerator mode use an S-function, which starts and stops instead of the referenced model.

For more information about model reference simulation modes, see Choose Simulation Modes for Model Hierarchies.

Related Topics