Block Callbacks
You can specify block callbacks that execute MATLAB® code for block events, such as when a block is loaded, opened, or edited.
On the Modeling tab, under Design, click Property Inspector.
Select the block whose callback you want to specify.
On the Properties tab of the Property Inspector, in the Callbacks section, select the callback you want to define.
In the box, enter the functions you want the callback to perform.
To specify a block callback programmatically, use set_param
to assign MATLAB code to the block callback parameter.
If a block callback executes before or after a modeling action takes place, that callback occurs immediately before or after the action.
Block Opening Callback Parameters
Block Callback Parameter | When Executed |
---|---|
When the block is opened. Generally, use this parameter with Subsystem blocks. The callback executes when you
double-click the block or when you use After you add an | |
After the block diagram is loaded. For
Subsystem blocks, the |
Block Editing Callback Parameters
Block Compilation and Simulation Callback Parameters
Block Callback Parameter | When Executed |
---|---|
Before the block diagram is compiled and before block parameters are
evaluated. For more information on | |
After the block diagram is compiled and before the simulation starts. In the case of an S-Function block,
| |
Before the simulation continues. | |
After the simulation pauses. | |
At any termination of the simulation. In the case of an
S-Function block, |
Block Saving and Closing Callback Parameters
Block Callback Parameter | When Executed |
---|---|
Before the block diagram is saved. For
Subsystem blocks, the | |
After the block diagram is saved. For
Subsystem blocks, the | |
When the block is closed using
The For example, to close all open MATLAB windows, use a command such as: set_param('my_model','CloseFcn','close all') | |
Before the block diagram is closed. When the model is closed,
the block's For Subsystem blocks,
the |
Subsystem Block Callback Parameters
You can use the other block callback parameters with Subsystem blocks, but the callback parameters in this table are specific to Subsystem blocks.
Note
A callback for a masked subsystem cannot directly reference the parameters of the
masked subsystem (see Create Block Masks). Simulink® evaluates block callbacks in the MATLAB 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. For example, here
gain
is the name of a mask parameter of the current block:
get_param(gcb, 'gain')
Block Callback Parameter | When Executed |
---|---|
After a block or line is deleted in a subsystem. If the block
has a | |
When an error has occurred in a subsystem. Use the following
form for the callback code for the newException = errorHandler(subsys, ... errorType, originalException) where
If you provide the original exception, then you do not need to specify the subsystem and the error type. The following
command sets the set_param(subsys,'ErrorFcn','errorHandler') set_param , do not include the input arguments of
the callback code. Simulink displays the error message returned by the callback. | |
Before closing a subsystem containing the block or when the block is made part of a new subsystem using either:
When you close the model, Simulink does not call the |