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 the set_param
function 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 Callback Executes |
---|---|
| 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 the After you add an |
| After the block diagram is loaded. For
Subsystem blocks, the |
Block Editing Callback Parameters
Block Callback Parameter | When Callback Executes |
---|---|
| When the block is moved or resized. |
| After a block name or path changes. When a
Subsystem block path changes, the Subsystem block
calls the |
PreCopyFcn | Before a block is copied. The If you copy a Subsystem block that contains a
block for which the The |
| After a block is copied. The callback is also executed if the
If you copy a Subsystem block that contains a
block for which the |
| When the block is copied or cut to the system clipboard. |
PreDeleteFcn | Before a block is graphically deleted. For example, when you graphically
delete the block or invoke the The |
| After a block is graphically deleted, for example, when you graphically
delete the block, invoke the When the
For Subsystem blocks, the
|
| When the block has been destroyed from memory, for example, when you
invoke the If the block was not previously graphically deleted, the
|
| When a block deletion is undone. |
Block Compilation and Simulation Callback Parameters
Block Callback Parameter | When Callback Executes |
---|---|
| Before the block diagram is compiled and before block parameters are
evaluated. For more information on the |
| After the block diagram is compiled and before the simulation starts. In the case of an S-Function block, the
|
| Before the simulation continues. |
| After the simulation pauses. |
| At any termination of the simulation. In the case of an
S-Function block, the |
Block Saving and Closing Callback Parameters
Block Callback Parameter | When Callback Executes |
---|---|
| 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 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 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 Author Block Masks. The software evaluates
block callbacks in the MATLAB base workspace, whereas the mask parameters reside in the masked subsystem's
private workspace. A block callback can use the get_param
function 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 Callback Executes |
---|---|
| After a block or line is deleted in a subsystem. If the block
has a |
| When an error has occurred in a subsystem. Use this 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. This command sets
the set_param(subsys,'ErrorFcn','errorHandler') set_param function, do not include the
input arguments of the callback code. The software 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, the software does not call the
|