Model Callbacks - MATLAB & Simulink (original) (raw)

Main Content

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 theDesign gallery, click Property Inspector.
  2. With no selection at the top level of your model or referenced model, on theProperties 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 Parameter When Callback Executes
PreLoadFcn Before the model is loaded.Do not use model parameters in aPreLoadFcn 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 theload_system function so that thePreLoadFcn callback executes.For an example, seeAutomatically 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 assolverAn error message for a model parameter added with add_paramProgrammatic 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, thePostLoadFcn 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.AvoidInitFcn 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 theInitFcn 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 theStopFcn 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 theDirty 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 andDeleteFcn block callbacks are called before theCloseFcn 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:

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 aStopFcn 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, 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, andStopFcn 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 theirRebuild 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 andStopFcn 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.

See Also

Topics