Dynamic Masked Subsystem

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 Lock and Unlock Libraries).

  2. Select the block in the library.

  3. On the Block tab, in the Mask group, click 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 Add Mask Code). Doing so triggers an error when you edit the parameter.

  6. Click Apply to apply the change or OK to apply the change and close 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:


  2. Specify that the block is self-modifying by using the following command:


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

Create Self-Modifying Mask

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

  1. In the Library window, on the Library tab, click Locked Library to unlock the library.

  2. On the Subsystem Block tab, in the Mask group, click Edit Mask. The Mask Editor opens.

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

  4. 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 is not selected, copy of the library block could not modify their structural contents. Also, 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

Suppose 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. You modify 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, Simulink does not evaluate myBlock, which lies under mySubsys, when the mask initialization code executes. Instead, Simulink only evaluates and updates the masked subsystem mySubsys. Meanwhile, myBlock remains unmodified.

You can force Simulink to evaluate such blocks earlier by using the Simulink.Block.eval method in the initialization code of the masked subsystem.


Related Topics