Documentation Center

  • Trial Software
  • Product Updates

Create Dynamic Masked Subsystems

Allow library block to modify its contents

This check box is enabled only if the masked subsystem resides in a library. Checking this option allows the block initialization code to modify the contents of the masked subsystem (that is, it lets the code add or delete blocks and set the parameters of those blocks). Otherwise, an error is generated when a masked library block tries to modify its contents in any way. To set this option at the MATLAB® prompt, select the self-modifying block and enter the following command.

set_param(gcb, 'MaskSelfModifiable', 'on');

Then save the block.

Create Self-Modifying Masks for Library Blocks

You can create masked library blocks that can modify their structural contents. These self-modifying masks allow you to:

  • Modify the contents of a masked subsystem based on parameters in the mask dialog box or when the subsystem is initially dragged from the library into a new model.

  • Vary the number of ports on a multiport S-Function block that resides in a library.

Creating Self-Modifying Masks Using the Mask Editor

To create a self-modifying mask using the Mask Editor:

  1. Unlock the library (see Modify and Lock Libraries).

  2. Right-click the block in the library.

  3. Select Mask > Edit Mask. The Mask Editor opens.

  4. In the Mask Editor Initialization pane, select the Allow library block to modify its contents option.

  5. Enter the code that modifies the masked subsystem in the mask Initialization pane.

    Do not enter code that structurally modifies the masked subsystem in a dialog parameter callback (see Mask Code Placement). Doing so triggers an error when a user edits the parameter.

  6. Click Apply to apply the change or OK to apply the change and dismiss the Mask Editor.

  7. Lock the library.

Creating Self-Modifying Masks from the Command Line

To create a self-modifying mask from the command line:

  1. Unlock the library using the following command:

    set_param(gcs,'Lock','off')
  2. Specify that the block is self-modifying by using the following command:

    set_param(block_name,'MaskSelfModifiable','on')

    where block_name is the full path to the block in the library.

Self-Modifying Mask Example

The library selfModifying_example selfModifying_example contains a masked subsystem that modifies its number of input ports based on a selection made in the subsystem mask dialog box.

Right-click the subsystem then select Mask > Edit Mask. The Mask Editor opens. The Mask Editor Parameters & Dialog pane defines a parameter numIn that stores the value for the Number of inports option. This mask dialog box callback adds or removes Input ports inside the masked subsystem based on the selection made in the Number of inports list.

To allow the dialog box callback to function properly, the Allow library block to modify its contents option on the Mask Editor Initialization pane is selected. If this option were not selected, copies of the library block could not modify their structural contents and changing the selection in the Number of inports list would produce an error.

Evaluate Blocks Under Self-Modifying Mask

This example shows how to force Simulink® to evaluate blocks inside self-modifying masks.

Simulink evaluates elements of models containing masks in the following order:

  1. Mask dialog box

  2. Mask initialization code

  3. Blocks or masked subsystems under the mask

Consider the following case:

A block named myBlock inside subsystem mySubsys masked by a self-modifying mask depends on mask parameter myParam to update itself.

myParam is exposed to the user through the Mask Parameters dialog box. mySubsys is updated through MATLAB code written in the Mask Initialization pane.

In this model, the sequence of updates is as follows:

  1. User modifies myParam through the mask dialog box.

  2. The mask initialization code receives this change and modifies mySubsys under the mask.

  3. myBlock, which lies under mySubsys, modifies itself based on the change to myParam.

In this sequence, myBlock, which lies under mySubsys, will not be evaluated when the mask initialization code executes. Instead, only the masked subsystem mySubsys is evaluated at that time and gets updated. Meanwhile, myBlock, which needs to change, remains unmodified.

You can force Simulink to evaluate such blocks earlier by using the Simulink.Mask.eval method in the masked subsystem's initialization code:

Simulink.Block.eval(mySubsys/myBlock);
Was this topic helpful?