Model Callbacks

Create Model Callbacks

You can create model callbacks interactively or programmatically.

To create a model callback interactively:

  1. In the Simulink® Editor, select File > Model Properties > Model Properties.

  2. In the Model Properties dialog box, select the Callbacks tab.

  3. In the left pane, select the callback parameter.

  4. In the right pane, enter the code to be invoked for the selected callback parameter.

To create a model callback programmatically, use the set_param command to assign MATLAB® code to a callback parameter. For example, the following command evaluates the variable testvar when you double-click the Test block in mymodel:

set_param('mymodel/Test', 'OpenFcn', 'testvar')

The execution of callbacks for model referencing reflects the order in which the top model and the model it references execute their callbacks. For example, suppose:

  • Model A references model B.

  • Model A has a OpenFcn callback that creates variables in the MATLAB workspace.

  • Model B has a CloseFcn callback that clears the MATLAB workspace.

Simulating model A triggers rebuilding the referenced model B.

When Simulink rebuilds model B, it opens and closes model B and 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 could use a StopFcn callback in model A to clear from the MATLAB workspace the variables used by the model.

View Model Callbacks

  1. In the Simulink Editor, select File > Model Properties > Model Properties.

  2. In the Model Properties dialog box, select the Callbacks tab.

    A list of callbacks that the model defines appear in the left pane of the dialog box, highlighted with an asterisk.

  3. To view the code for a callback that has an asterisk, click the callback.

    The MATLAB code appears in the edit box on the right pane of the dialog box. In this example, StopFcn* is one of the defined callbacks for this model. Clicking StopFcn* displays the user-defined code for that callback.

Model Callback Parameters

 Model Loading and Closing Callback Parameters

 Model Saving Callback Parameters

 Model Simulation Callback Parameters

Was this topic helpful?