Main Content

Model Callbacks

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 theInitFcncallback for a library, which is called as part of simulation, because you cannot simulate a library.

Create Model Callbacks

  1. In the Simulink®Editor, open the Property Inspector. In theModelingtab, underDesign, clickProperty Inspector.

  2. With no selection at the top level of your model, in thePropertiestab, in theCallbackssection, select the callback you want to set.

  3. In the box, enter the functions you want the callback to perform.

To create a model callback programmatically, use theset_paramfunction to assign MATLAB®code to a model callback parameter. SeeModel Callback Parameters

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 aPostLoadFcncallback that creates variables in the MATLAB workspace.

    • Has theRebuildconfiguration parameter set toAlways,If changes detected, orIf any changes in known dependencies detected.

  • Model B:

    • Has aCloseFcncallback that clears the MATLAB workspace.

    • Has not been built or is out of date.

Simulating model A triggers rebuilding the referenced model B. When Simulink rebuilds model B, it opens and closes model B, which invokes the model BCloseFcncallback.CloseFcnclears the MATLAB workspace, including the variables created by the model AOpenFcncallback.

Instead of using aCloseFcncallback for model B, you can use aStopFcncallback 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.

If a model references multiple instances of the same model in normal mode, callbacks execute for each instance.

For models referenced in accelerator mode, Simulink does not execute some callbacks. If everything is up to date and theRebuildconfiguration parameter is set toIf any changes in known dependencies detected, then the referenced model does not compile and itsInitFcncallbacks do not execute. Callbacks such asStartFcnandStopFcndo not execute because referenced models in accelerator mode use an S-function, which starts and stops instead of the referenced model.

Note

Simulation outputs are not available in theStopFcncallbacks for command-line simulations.

ModelInitFcncallbacks that edit the structure of the model must be avoided. Simulink may sometimes execute or ignore based on the model load status.

Model Callback Parameters

Model Loading and Closing Callback Parameters

Model Saving Callback Parameters

Model Simulation Callback Parameters

Related Topics