Main Content

Mask Callback Code

Add Mask Code

You can use MATLAB® code to initialize a mask and to draw mask icons. Since the location of code affects model performance, add your code to reflect the functionality you need.

PurposeAdd in Mask EditorProgrammatic Specification
Initialize the maskCode paneInitialization parameter
Draw mask iconIcon paneDisplay parameter
Callback code for mask parametersParameters & Dialog paneCallbackFile parameter

Execute Drawing Command

Place MATLAB code for drawing mask icons in the Icon Drawing Commands section of the Icon pane. Simulink® executes these commands sequentially to redraw the mask icon when:

  • Block is rendered first on the Mask Editor canvas.

  • Mask parameters and values that depend on drawing commands change.

  • Block appearance is altered due to rotation or other changes.

Note

Placing MATLAB code for drawing mask icons in the Initialization section affects model performance. This behavior is because Simulink redraws the icon each time the masked block is evaluated in the model.

Execute Initialization Command

Initialization commands for all masked blocks in a model run when you:

  • Update the diagram

  • Start simulation

  • Start code generation

  • Apply mask changes

  • Change any of the parameters that define the mask, such as MaskDisplay and MaskInitialization, using the Mask Editor or set_param.

  • Rotate or flip the masked block, if the icon depends on initialization commands.

  • Cause the icon to be drawn or redrawn, and the icon drawing depends on initialization code.

  • Change the value of a mask parameter by using the block dialog box or set_param.

When you open a model, Simulink locates visible masked blocks that reside at the top level of the model or in an open subsystem.

Simulink only executes the initialization commands of these visible masked blocks as well as their parent block if they meet either of the following conditions:

  • The masked block has icon drawing commands.

    Note

    Simulink does not initialize masked blocks that do not have icon drawing commands, even if they have initialization commands during model load.

  • The masked subsystem belongs to a library and has the Allow library block to modify its contents parameter enabled.

When you load a model into memory without displaying the model graphically, initialization commands are not run for the masked blocks except for library blocks with self-modifiable mask. See Load a Model and load_system for information about loading a model without displaying it.

Note

The non-tunable parameters of a masked block are not evaluated if the model is already compiled (initialized).

Execute Callback Code

Mask parameter callback codes are executed in a temporary workspace and not in the base workspace. If you need a variable created in the callback to be available later (not during callback processing), you must explicitly assign those variables to the base workspace.

Simulink executes the callback commands when:

  • You open the mask dialog box. Callback commands execute sequentially, starting with the top mask dialog box.

  • You modify a parameter value in the mask dialog box and then change the cursor location. For example, you press the Tab key or click into another field in the dialog box after changing the parameter value.

  • You modify the parameter value by using the set_param command, the callback commands execute.

  • You modify the parameter value, either in the mask dialog box or using set_param, and then apply the change by clicking Apply or OK. Mask initialization commands execute after callback commands. For more information, see Code Pane.

  • You hover over a masked block to see the tool tip for the block, when the tool tip contains parameter names and values.

    Note

    Callback commands do not execute if the mask dialog box is open when the block tool tip appears.

  • You update a diagram by pressing Ctrl+D or by clicking Update Model on the Modeling tab in the Simulink Editor.

  • If you close a mask dialog box without saving the changes, the Callback command for parameters is executed sequentially.

Note

Buttons on mask dialog box are unavailable when the callback code associated with the button is being executed.

Related Topics