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, port, 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 the path changes.
Tracing Callbacks
Callback tracing allows you to determine the callbacks Simulink invokes and in what order Simulink invokes them when you open or simulate a model. To enable callback tracing, select the Callback tracing option on the Simulink Preferences dialog box (see Setting Simulink Preferences) or execute set_param(0, 'CallbackTracing', 'on')
. This option causes Simulink to list callbacks in the MATLAB command window as they are invoked.
Creating Model Callback Functions
You can create model callback functions interactively or programmatically. Use the Callbacks pane of the model's Model Properties dialog box (see Callbacks Pane) to create model callbacks interactively. To create a callback programmatically, use the set_param
command to assign a MATLAB expression that implements the function to the model parameter corresponding to the callback (see Model Callback Parameters).
For example, this command evaluates the variable testvar
when the user double-clicks the Test block in mymodel
.
You can examine the clutch
system (clutch.mdl
) for routines associated with many model callbacks.
Model Callback Parameters
The following table lists the model parameters used to specify model callback routines and indicates when the corresponding callback routines are executed.
Creating Block Callback Functions
You can create model callback functions interactively or programmatically. Use the Callbacks pane of the model's Block Properties dialog box (see Callbacks Pane) to create model callbacks interactively. To create a callback programmatically, use the set_param
command to assign a MATLAB expression that implements the function to the block parameter corresponding to the callback (see Block Callback Parameters).
Note
A callback for a masked subsystem cannot directly reference the parameters of the masked subsystem (see About Masks). The reason? Simulink evaluates block callbacks in a model's base workspace whereas the mask parameters reside in the masked subsystem's private workspace. A block callback, however, can use get_param to obtain the value of a mask parameter, e.g., get_param(gcb, 'gain'), where gain is the name of a mask parameter of the current block.
|
Block Callback Parameters
This table lists the parameters for which you can define block callback routines, and indicates 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 the 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 the 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 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 in the "Model Creation Commands" section of the Simulink online help). |
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 "S-Function Callback Methods" in 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 "S-Function Callback Methods" in Writing S-Functions for more information. |
UndoDeleteFcn |
When a block delete is undone. |
Block input and output ports have a single callback parameter, ConnectionCallback
. This parameter allows you to set callbacks on ports that are triggered every time the connectivity of those ports changes. Examples of connectivity changes include deletion of blocks connected to the port and deletion, disconnection, or connection of branches or lines to the port.
Use get_param
to get the port handle of a port and set_param
to set the callback on the port. For example, suppose the currently selected block has a single input port. The following code fragment sets foo as the connection callback on the input port.
The first argument of the callback function must be a port handle. The callback function can have other arguments (and a return value) as well. For example, the following is a valid callback function signature.
![]() | Discretizing a Model from the MATLAB Command Window | Managing Model Versions | ![]() |