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 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 ParameterWhen Executed

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 open_system with the block as an argument. The OpenFcn parameter 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 OpenFcn for include defining variables for a block, making a call to MATLAB 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 Executed

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 is also executed if add_block 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 block CopyFcn callback is called after all PreCopyFcn callbacks are executed.

CopyFcn

After a block is copied. The callback is also executed if add_block 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 delete_block on the block).

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

DeleteFcn

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

When the DeleteFcn is called, the block handle is still valid and can be accessed using get_param. If the block is graphically deleted by invoking delete_block or by closing the model, after deletion the block is destroyed from memory and the block's DestroyFcn 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 delete_block 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 blockDeleteFcn callback is called prior to the DestroyFcn. When the DestroyFcn 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 Executed

InitFcn

Before the block diagram is compiled and before block parameters are evaluated. For more information on 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, StartFcn 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, StopFcn 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 Executed

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 close_system.

The CloseFcn 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 close_system.

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 block's ModelCloseFcn is called prior to its DeleteFcn.

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). 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 ParameterWhen Executed

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 the following form for the callback code for the ErrorFcn parameter:

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 (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.

The following command sets the ErrorFcn of the subsystem subsys to call the errorHandler callback:

set_param(subsys,'ErrorFcn','errorHandler')
In such calls to set_param, do not include the input arguments of the callback code. Simulink 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, Simulink does not call the ParentCloseFcn callbacks of blocks at the root model level.

Related Topics