Create Library Blocks Using Self-Modifiable Masks and Variant Blocks - MATLAB & Simulink (original) (raw)

This example shows how you can modify the contents of a linked block without disabling the link to the referenced library block. You can either use a library block with a self-modifiable mask, or a subsystem with variant blocks and make changes using the mask parameters in the linked block.

Explore the Model

The model slexMaskSelfModifiableExample uses two linked blocks, Variants blocks and Self-Modifying block, from the slexMaskSelfModifyingLib library. Both linked blocks have masks with a Time domain parameter to select between discrete and continuous time integrators.

Create Masked Subsystem with Variant Blocks

You can use variant blocks to store all design alternatives of a system in a single subsystem and then use a mask parameter as a variant control to switch between these alternatives.

To create a masked subsystem with variant blocks, use these steps:

1. Open the library slexMaskSelfModifyingLib.

2. Use any of these options to unlock the library:

set_param(gcs,"Lock","off")

3. Open the Mask Editor to edit the mask of the Subsystem block Variants blocks. In the Parameters & Dialog tab of the Mask Editor, add a popup parameter with prompt Time domain: and name TimeDomain.

4. In the Property Editor pane of Mask Editor, double-click the Type options field. In the dialog box that opens, select options Use Enumeration and Create new Enumeration. Enter the enumeration class name as TimeDomainForVariant with Name and Description specified as Continuous and Discrete, respectively. Click Save. These enumeration class values define the variant control expression of the variant blocks.

5. Click Save Mask and close the Mask Editor.

6. Select Variants blocks and then in the Mask section of the Block tab of the Simulink Toolstrip, click Look Under Mask. Variants blocks is preconfigured with two implementations of the integrator system using Variant Start and Variant End blocks. Make these settings based on the name and enumeration class of the mask popup parameter TimeDomain:

7. Lock the library.

The model slexMaskSelfModifiableExample uses the library block Variants blocks to switch between continuous and discrete time integrators using the mask dialog box of the linked block.

Create Self-Modifiable Masks for Library Blocks

Adding a block with a self-modifiable mask from a library to a model creates a self-modifiable linked block. You can make dynamic structural changes in a self-modifiable linked block by adding a MATLAB® code in the Initialization pane of the Mask Editor. Simulink runs this mask initialization code for a self-modifiable library block when you load the block.

To create a self-modifiable mask of a library block, use these steps:

1. Open and unlock the library slexMaskSelfModifyingLib. You can use any of these options to unlock the library:

set_param(gcs,"Lock","off")

2. Open the Mask Editor of Self-Modifying block. In the Parameters & Dialog tab of the Mask Editor, add a popup parameter with prompt Time domain:, name time, and Type options Continuous and Discrete.

3. In the Code tab of the Mask Editor, select Allow mask initialization code to modify the subsystem's content. This enables the initialization code to modify the contents of the masked subsystem.

You can also specify that a block is self-modifiable by using this command:

set_param(gcb,"MaskSelfModifiable","on")

4. In the Code tab of the Mask Editor, enter the mask initialization code to modify the masked subsystem. When you change the value of the time parameter in the mask dialog box, the mask initialization code dynamically switches between a continuous or discrete-time integrator block. For example, if you specify the Time domain parameter as Discrete, the Integrator block is replaced with a Discrete-Time Integrator block.

Do not enter a code that structurally modifies a masked subsystem in a dialog parameter callback. Doing so triggers an error when you edit the parameter. For more information, see When Does Mask Callback Code Execute?

5. Click Save Mask and close the Mask Editor.

6. Lock the library.

The model slexMaskSelfModifiableExample uses the library block Self-Modifying block to dynamically replace the integrator block using the mask dialog box of the linked block.

See Also

Blocks

Topics