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.
- Self-modifiable mask — To make structural changes in a linked subsystem, add mask initialization code in the library block and select Allow mask initialization code to modify the subsystem's content in the Mask Editor. These settings create a self-modifiable mask. When you add a library block with a self-modifiable mask in a model, you can make dynamic structural changes in the linked block. The changes include adding or removing blocks, setting parameters for parent and child blocks, or varying the number of ports in a multiport S-Function linked block.
- Variant blocks — Variant blocks can represent multiple structural or algorithmic implementations of a system component in a single model and allow you to switch between them based on specific conditions. When you add a masked subsystem with variant blocks from a library to a model, you can select a variant choice in the linked block by using the mask dialog box parameters. For optimal performance, use variant blocks when you can define all potential configurations of a system as variant choices, because they do not require executing mask initialization codes. For more information, see What Are Variants and When to Use Them.
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:
- In the Library window, click the lock badge in the lower-left corner.
- In the Library tab of the Simulink toolstrip, click Locked Library.
- Use this command in the Command window:
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
:
- In the Block Parameters dialog box of the Variant Start block, specify: Variant Start End Tag as
TimeDomainVariant
and Variant control mode asexpression
. Under Variant control expression, add the expressions asTimeDomain == TimeDomainForVariant.Continuous
andTimeDomain == TimeDomainForVariant.Discrete
.
- In the Block Parameters dialog box of the Variant End block, specify: Variant Start End Tag as
TimeDomainVariant
.
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:
- In the Library window, click the lock badge in the lower-left corner.
- In the Library tab of the Simulink toolstrip, click Locked Library.
- Use this command in the Command window:
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.