メインコンテンツ

Modify Structure of Subsystem Blocks Using Self- Modifiable Mask

You can modify the internal structure of a masked Subsystem block through self-modifiable masks. Blocks or ports get added to a Subsystem block dynamically based on the value of mask parameters.

Create Dynamic Masked Subsystems Using Self-Modifiable Mask

There are scenarios where you want to modify the contents of a subsystem based on the value of a mask parameter. The modifications include adding a new block or changing the number of ports. To modify the structure, mask the subsystem and use mask initialization code to alter its contents. However, there is a limitation with linked library blocks, any linked block created from the library will restrict modifications to the contents of the linked subsystem. To address this limitation use self-modifiable masks. When you mark the masked subsystem as self-modifiable, it allows you to alter the contents of its linked subsystem without breaking the library link. Dynamic structural changes are made safely and effectively in this method.

Use a self-modifiable mask, if the design variations,

  • Change dynamically based on the specified mask parameter values.

  • Are large and complex to manage using other Simulink features such as variants.

Explore the Model

The example model slexMaskSelfModifiableExample.slx uses linked library blocks from the library slexMaskSelfModifiableLib.slx. In the example model, refer to the section Self-Modifiable Mask. The linked library block SumOfNaturalNumbers calculates the sum of the kth powers of the first n natural numbers. This block includes two mask parameters, numbers n and power k. The mask initialization code defined in the callback file sumOfNumbersCallback dynamically creates n Constant blocks and assigns the value k to the block Power_constant as part of executing mask initialization code.

The linked library block AvgSensors calculates the average of multiple sensor inputs. The number of sensors corresponding to the number of inports on the AvgSensors block is added dynamically based on the mask parameter numSensors. This configuration is managed through mask initialization code defined in the callback file numberOfSensors.

The model slexMaskSelfModifiableExample.slx uses variant blocks from the library slexMaskSelfModifiableLib.slx that demonstrates how to use variant blocks as an alternative to self-modifiable masks. The variant blocks contain a mask popup parameter that uses an enumeration to define the choice of the time domain. When you select Continuous then the Integrator variant is active, otherwise Discrete-Time Integrator variant is active.

Add Blocks and Ports Dynamically Inside the Masked Subsystem

You can add blocks and ports dynamically using mask initialization code to a Subsystem library block. Create linked library blocks in a model to achieve structural changes.

Add Blocks Dynamically

Create a library slexMaskSelfModifiableLib.slx with a masked block SumOfNaturalNumbers to calculate the sum of kth powers of first n natural numbers.

1. In the Parameters and Dialog tab of the Mask Editor, create two mask parameters k and n.

2. In the Code tab, select the option Allow mask initialization code to modify the subsystem's content to mark the Subsystem block as self-modifiable.

3. Specify the name of the callback file in the Callback Organization section of the Code tab to create a callback file sumOfNumbersCallback that saves the mask initialization code.

4. Save the mask.

Add Ports to a Block Dynamically

You can also dynamically add ports to a masked Subsystem block. To create a block AvgSensors that calculates the average of multiple sensor inputs:

1. In the same library slexMaskSelfModifiableLib.slx, create a masked Subsystem block AvgSensors.

2. In the Parameters and Dialog tab of the Mask Editor, create a mask parameter numSensors that corresponds to the number of input ports in the AvgSensors Subsystem block.

3. In the Code tab of the Mask Editor, select the option Allow mask initialization code to modify the subsystem's content to mark the Subsystem block as self-modifiable.

4. Specify the name of the callback file in the Callback Organization section of the Code tab to create a callback file numberOfSensors that saves the mask initialization code.

5. Save the mask

Verify Self-Modifiable Property

The library slexMaskSelfModifiableLib.slx is created with two blocks SumOfNaturalNumbers and AvgSensors in the previous sections. To use these library blocks in your model:

1. Create a model slexMaskSelfModifiableExample.slx.

2. Create linked blocks from library blocks SumOfNaturalNumbers and AvgSensors in slexMaskSelfModifiableExample.slx.

3. Add the Inport and Display blocks as needed.

4. Specify the values for k and n in the mask dialog box of SumOfNaturalNumbers.

5. Click Look Inside Mask to observe the changes to the structure of the block.

6. Similarly specify the numSensors in the mask dialog box of AvgSensors.

7. Observe that the number of ports are generated corresponding to the mask parameter numSensors.

8. Create corresponding Constant blocks and connect the ports lines.

9. Simulate the model and observe the results.

Limitations

Although self-modifiable masks help to dynamically modify the structure of a Subsystem block, it needs to be used thoughtfully because of these limitations:

  • Self-modifiable masks modify the structure of the block during simulation, which can affect performance. Instead you can use variant systems when you have few design variations.

  • Do not make the mask self-modifiable to set the value of child block parameter from parent mask initialization code. It results in losing tunability of child block parameter in the generated code. For example, in this code, the child block parameter childParam becomes nontunable in the generated code.

value = get_param(<parentBlock>, "parentMaskparam")
set_param(<childBlock>, "childParam", value)

You can either promote the child block parameter to the parent block to set its value or reference the parent mask parameter in the child block.

  • It is hard to understand the block logic if it is written using MATLAB code.

Use Variant Systems as an Alternate to Self-Modifiable Masks

Structural modifications to a subsystem using mask initialization code are computationally expensive and difficult to manage. If the design variations are known in advance and are based on mask parameter values, it is recommended to use Simulink variant systems. Simulink variants provide an efficient way to switch between different design variations using a variant control expression. By using variant blocks, you can design multiple logical paths but only one of the logical paths is active and executed at any given time based on the variant control expression. You can associate the variant control expression to a mask parameter. For example, the logic inside a subsystem needs to vary depending on whether the model operates in a continuous or discrete time domain. A mask dialog parameter captures the user input specifying the desired domain. Based on this value, Simulink variants can automatically activate the appropriate set of blocks.

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

Insert a Subsystem block named Variants blocks in slexMaskSelfModifiableLib.slx to implement continuous and discrete time domain.

1. In the Parameters and Dialog tab of the Mask Editor, create a popup parameter TimeDomain.

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

3. Click Save Mask and close the Mask Editor.

4. Select Variants blocks and then click Look Inside 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:

a. In the Block Parameters dialog box of the Variant Start block, specify Variant Start End Tag as TimeDomainVariant and Variant control mode as expression. Under Variant control expression, add the expressions as TimeDomain == TimeDomainForVariant.Continuous and TimeDomain == TimeDomainForVariant.Discrete.

b. In the Block Parameters dialog box of the Variant End block, specify Variant Start End Tag as TimeDomainVariant.

View Simulation Result

The Variants blocks subsystem was created in the library slexMaskSelfModifiableLib.slx in the previous section.

1. In the model slexMaskSelfModifiableExample.slx, create linked library block Variants blocks from the library.

2. In the mask dialog box, select Continuous.

3. Click Look Inside Mask and observe the changes to the block.

4. Simulate the model and observe that the continuous integrator variant is activated.

See Also

Blocks

Topics