Using Simulink    

Using Callback Routines

You can define MATLAB expressions that execute when the block diagram or a block is acted upon in a particular way. These expressions, called callback routines, are associated with block or model parameters. For example, the callback associated with a block's OpenFcn parameter is executed when the model user double-clicks on that block's name or path changes.

To define callback routines and associate them with parameters, use the set_param command (see set_param).

For example, this command evaluates the variable testvar when the user double-clicks on the Test block in mymodel:

You can examine the clutch system (clutch.mdl) for routines associated with many model callbacks.

Tracing Callbacks

Callback tracing allows you to determine which callbacks Simulink invokes and in what order Simulink invokes them when you open or simulate a model. To enable callback tracking, select the Callback tracing option on the Simulink Preferences dialog box (see Setting Simulink Preferences) or execute set_param(0, 'CallbackTracing', 'on'). This options causes Simulink to list callbacks in the MATLAB command window as they are invoked.

Model Callback Parameters

This table lists the parameters for which you can define model callback routines, and indicate when those callback routines are executed. Routines that are executed before or after actions take place occur immediately before or after the action.

Parameter
When Executed
CloseFcn
Before the block diagram is closed.
PostLoadFcn
After the model is loaded. Defining a callback routine for this parameter might be useful for generating an interface that requires that the model has already been loaded.
InitFcn
Called at start of model simulation.
PostSaveFcn
After the model is saved.
PreLoadFcn
Before the model is loaded. Defining a callback routine for this parameter might be useful for loading variables used by the model.
PreSaveFcn
Before the model is saved.
StartFcn
Before the simulation starts.
StopFcn
After the simulation stops. Output is written to workspace variables and files before the StopFcn is executed.

Block Callback Parameters

This table lists the parameters for which you can define block callback routines, and indicate when those callback routines are executed. Routines that are executed before or after actions take place occur immediately before or after the action.

Parameter
When Executed
CloseFcn
When the block is closed using the close_system command.
CopyFcn
After a block is copied. The callback is recursive for Subsystem blocks (that is, if you copy a Subsystem block that contains a block for which the CopyFcn parameter is defined, the routine is also executed). The routine is also executed if an add_block command is used to copy the block.
DeleteFcn
Before a block is deleted. This callback is recursive for Subsystem blocks.
DestroyFcn
When block has been destroyed.
InitFcn
Before the block diagram is compiled and before block parameters are evaluated.
LoadFcn
After the block diagram is loaded. This callback is recursive for Subsystem blocks.
ModelCloseFcn
Before the block diagram is closed. This callback is recursive for Subsystem blocks.
MoveFcn
When block is moved or resized.
NameChangeFcn
After a block's name and/or path changes. When a Subsystem block's path is changed, it recursively calls this function for all blocks it contains after calling its own NameChangeFcn routine.
OpenFcn
When the block is opened. This parameter is generally used with Subsystem blocks. The routine is executed when you double-click on the block or when an open_system command is called with the block as an argument. The OpenFcn parameter overrides the normal behavior associated with opening a block, which is to display the block's dialog box or to open the subsystem.
ParentCloseFcn
Before closing a subsystem containing the block or when the block is made part of a new subsystem using the new_system command (see new_system).
PreSaveFcn
Before the block diagram is saved. This callback is recursive for Subsystem blocks.
PostSaveFcn
After the block diagram is saved. This callback is recursive for Subsystem blocks.
StartFcn
After the block diagram is compiled and before the simulation starts. In the case of an S-Function block, StartFcn executes immediately before the first execution of the block's mdlProcessParameters function. See "Overview of the C MEX S-Function Routines" in Chapter 3 of Writing S-Functions for more information.
StopFcn
At any termination of the simulation. In the case of an S-Function block, StopFcn executes after the block's mdlTerminate function executes. See "Overview of the C MEX S-Function Routines" in Chapter 3 of Writing S-Functions for more information.
UndoDeleteFcn
When a block delete is undone.

Parameter
When Executed
CloseFcn
When the block is closed using the close_system command.
CopyFcn
After a block is copied. The callback is recursive for Subsystem blocks (that is, if you copy a Subsystem block that contains a block for which the CopyFcn parameter is defined, the routine is also executed). The routine is also executed if an add_block command is used to copy the block.
DeleteFcn
Before a block is deleted. This callback is recursive for Subsystem blocks.
DestroyFcn
When block has been destroyed.
InitFcn
Before the block diagram is compiled and before block parameters are evaluated.
LoadFcn
After the block diagram is loaded. This callback is recursive for Subsystem blocks.
ModelCloseFcn
Before the block diagram is closed. This callback is recursive for Subsystem blocks.
MoveFcn
When block is moved or resized.
NameChangeFcn
After a block's name and/or path changes. When a Subsystem block's path is changed, it recursively calls this function for all blocks it contains after calling its own NameChangeFcn routine.
OpenFcn
When the block is opened. This parameter is generally used with Subsystem blocks. The routine is executed when you double-click on the block or when an open_system command is called with the block as an argument. The OpenFcn parameter overrides the normal behavior associated with opening a block, which is to display the block's dialog box or to open the subsystem.
ParentCloseFcn
Before closing a subsystem containing the block or when the block is made part of a new subsystem using the new_system command (see new_system).
PreSaveFcn
Before the block diagram is saved. This callback is recursive for Subsystem blocks.
PostSaveFcn
After the block diagram is saved. This callback is recursive for Subsystem blocks.
StartFcn
After the block diagram is compiled and before the simulation starts. In the case of an S-Function block, StartFcn executes immediately before the first execution of the block's mdlProcessParameters function. See "Overview of the C MEX S-Function Routines" in Chapter 3 of Writing S-Functions for more information.
StopFcn
At any termination of the simulation. In the case of an S-Function block, StopFcn executes after the block's mdlTerminate function executes. See "Overview of the C MEX S-Function Routines" in Chapter 3 of Writing S-Functions for more information.
UndoDeleteFcn
When a block delete is undone.


 Controlling Access to Subsystems Tips for Building Models