Main Content

Block Callbacks

You can specify block callbacks that execute MATLAB® code for block events, such as when a block is loaded, opened, or edited.

  1. On the Modeling tab, under Design, click Property Inspector.

  2. Select the block whose callback you want to specify.

  3. On the Properties tab of the Property Inspector, in the Callbacks section, select the callback you want to define.

  4. 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 ParameterWhen Callback Executes

OpenFcn

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 open_system function with the block as an argument. The OpenFcn callback overrides the normal behavior associated with opening a block, which is to display the block dialog box or to open the subsystem. Examples of tasks that you can use the OpenFcn callback for include defining variables for a block, making a MATLAB call to produce a plot of simulated data, or generating a graphical user interface.

After you add an OpenFcn callback to a block, double-clicking the block does not open the block dialog box. Also, the block parameters do not appear in the Property Inspector when the block is selected. To set the block parameters, select Block Parameters from the block context menu.

LoadFcn

After the block diagram is loaded.

For Subsystem blocks, the LoadFcn callback is performed for any blocks in the subsystem, including other Subsystem blocks, that have a LoadFcn callback defined.

Block Editing Callback Parameters

Block Callback ParameterWhen Callback Executes

MoveFcn

When the block is moved or resized.

NameChangeFcn

After a block name or path changes.

When a Subsystem block path changes, the Subsystem block calls the NameChangeFcn callback of its descendant blocks and then calls the NameChangeFcn callback on itself.

PreCopyFcn

Before a block is copied. The PreCopyFcn callback is also executed if the add_block function is used to copy the block.

If you copy a Subsystem block that contains a block for which the PreCopyFcn callback is defined, that callback executes also.

The CopyFcn block callback is called after all PreCopyFcn callbacks are executed.

CopyFcn

After a block is copied. The callback is also executed if the add_block function is used to copy the block.

If you copy a Subsystem block that contains a block for which the CopyFcn parameter is defined, the callback is also executed.

ClipboardFcn

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 delete_block function on the block.

The PreDeleteFcn callback is not called when the model containing the block is closed. The DeleteFcn block callback is called after the PreDeleteFcn callback, unless the PreDeleteFcn callback invokes the error command, either explicitly or via a command used in the PreDeleteFcn callback.

DeleteFcn

After a block is graphically deleted, for example, when you graphically delete the block, invoke the delete_block function on the block, or close the model containing the block.

When the DeleteFcn callback is called, the block handle is still valid and can be accessed using the get_param function. If the block is graphically deleted by invoking the delete_block function or by closing the model, after deletion the block is destroyed from memory and the DestroyFcn block callback is called.

For Subsystem blocks, the DeleteFcn callback is performed for any blocks in the subsystem, including other Subsystem blocks, that have a DeleteFcn callback defined.

DestroyFcn

When the block has been destroyed from memory, for example, when you invoke the delete_block function on either the block or a subsystem containing the block or close the model containing the block.

If the block was not previously graphically deleted, the DeleteFcn block callback is called prior to the DestroyFcn callback. When the DestroyFcn callback is called, the block handle is no longer valid.

UndoDeleteFcn

When a block deletion is undone.

Block Compilation and Simulation Callback Parameters

Block Callback ParameterWhen Callback Executes

InitFcn

Before the block diagram is compiled and before block parameters are evaluated. For more information on the InitFcn callback, see Initialization Function.

StartFcn

After the block diagram is compiled and before the simulation starts.

In the case of an S-Function block, the StartFcn callback executes immediately before the first execution of the block’s mdlProcessParameters function. For more information, see S-Function Callback Methods.

ContinueFcn

Before the simulation continues.

PauseFcn

After the simulation pauses.

StopFcn

At any termination of the simulation.

In the case of an S-Function block, the StopFcn callback executes after the block's mdlTerminate function executes. For more information, see S-Function Callback Methods.

Block Saving and Closing Callback Parameters

Block Callback ParameterWhen Callback Executes

PreSaveFcn

Before the block diagram is saved.

For Subsystem blocks, the PreSaveFcn callback is performed for any blocks in the subsystem, including other Subsystem blocks, that have a PreSaveFcn callback defined.

PostSaveFcn

After the block diagram is saved.

For Subsystem blocks, the PostSaveFcn callback is performed for any blocks in the subsystem, including other Subsystem blocks, that have a PostSaveFcn callback defined.

CloseFcn

When the block is closed using the close_system function.

The CloseFcn callback is not called when you interactively close the block parameters dialog box, when you interactively close the subsystem or model containing the block, or when you close the subsystem or model containing a block using the close_system function.

For example, to close all open MATLAB windows, use a command such as:

set_param('my_model','CloseFcn','close all')

ModelCloseFcn

Before the block diagram is closed.

When the model is closed, the ModelCloseFcn block callback is called prior to the DeleteFcn and DestroyFcn block callbacks, if either are set.

For Subsystem blocks, the ModelCloseFcn callback is performed for any blocks in the subsystem, including other Subsystem blocks, that have a ModelCloseFcn callback defined.

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 ParameterWhen Callback Executes

DeleteChildFcn

After a block or line is deleted in a subsystem.

If the block has a DeleteFcn or DestroyFcn callback, those callbacks execute prior to the DeleteChildFcn callback.

ErrorFcn

When an error has occurred in a subsystem.

Use this form for the callback code for the ErrorFcn callback:

newException = errorHandler(subsys,...
errorType,originalException)

where

  • errorHandler is the name of the function.

  • subsys is a handle to the subsystem in which the error occurred.

  • errorType is a character vector indicating the type of error that occurred.

  • originalException is an MSLException. For more information, see Error Handling in Simulink Using MSLException Objects.

  • newException is an MSLException specifying the error message to be displayed to the user.

If you provide the original exception, then you do not need to specify the subsystem and the error type.

This command sets the ErrorFcn callback of the subsystem subsys to call the errorHandler callback.

set_param(subsys,'ErrorFcn','errorHandler')
In such calls to the set_param function, do not include the input arguments of the callback code. The software displays the error message returned by the callback.

ParentCloseFcn

Before closing a subsystem containing the block or when the block is made part of a new subsystem using either:

  • The new_system function

  • The Create Subsystem icon on the Multiple tab, in the Simulink® Editor.

When you close the model, the software does not call the ParentCloseFcn callbacks of blocks at the root model level.

Related Topics