0
0
SimulinkHow-ToBeginner · 4 min read

How to Create a Masked Subsystem in Simulink: Step-by-Step Guide

To create a masked subsystem in Simulink, first create a subsystem block, then right-click it and select Mask > Create Mask. This opens the Mask Editor where you can add parameters, customize the icon, and define initialization code to control the subsystem's behavior.
📐

Syntax

Creating a masked subsystem involves these main steps:

  • Create Subsystem: Group blocks into a subsystem.
  • Mask Creation: Right-click subsystem > Mask > Create Mask.
  • Mask Editor: Add parameters, customize icon, and write initialization code.
  • Apply Mask: Save and close the editor to apply the mask.

This process lets you hide internal details and expose only needed parameters.

plaintext
1. Select blocks and create subsystem (Ctrl+G or right-click > Create Subsystem)
2. Right-click subsystem > Mask > Create Mask
3. In Mask Editor, use tabs:
   - Parameters: Add input fields
   - Icon & Ports: Draw icon using commands
   - Initialization: Write MATLAB code to run on mask update
4. Click OK to apply mask
💻

Example

This example shows how to create a masked subsystem with one parameter and a custom icon.

matlab
%% Step 1: Create a simple subsystem
open_system(new_system('exampleModel'));
add_block('simulink/Commonly Used Blocks/Constant','exampleModel/Constant');
add_block('simulink/Commonly Used Blocks/Gain','exampleModel/Gain');
add_block('simulink/Commonly Used Blocks/Scope','exampleModel/Scope');
add_line('exampleModel','Constant/1','Gain/1');
add_line('exampleModel','Gain/1','Scope/1');
Simulink.SubSystem.createSubsystem({'exampleModel/Constant','exampleModel/Gain','exampleModel/Scope'},'exampleModel/MySubsystem');

%% Step 2: Create mask programmatically
maskObj = Simulink.Mask.create('exampleModel/MySubsystem');

%% Step 3: Add parameter to mask
param = maskObj.addParameter('Type','edit','Name','GainValue','Prompt','Gain Value','Value','2');

%% Step 4: Set initialization code to update Gain block
maskObj.Initialization = 'set_param([gcb ''/Gain''], ''Gain'', GainValue);';

%% Step 5: Customize icon drawing
maskObj.IconDrawing = 'plot([-1 1],[0 0],''k''); text(0,0,''My Mask'',''HorizontalAlignment'',''center'');';

%% Save and open model
save_system('exampleModel');
open_system('exampleModel/MySubsystem');
Output
A new Simulink model named 'exampleModel' opens with a masked subsystem named 'MySubsystem'. The mask shows a text icon 'My Mask' and has a parameter 'GainValue' that controls the Gain block inside the subsystem.
⚠️

Common Pitfalls

  • Not updating internal blocks: Forgetting to write initialization code to link mask parameters to internal blocks causes parameters to have no effect.
  • Incorrect icon commands: Using invalid icon drawing commands can cause errors or no icon display.
  • Parameter name mismatch: Parameter names in the mask must match those used in initialization code exactly.
  • Masking non-subsystem blocks: Only subsystems can be masked; trying to mask other blocks will fail.
matlab
%% Wrong way: No initialization code
maskObj.Initialization = '';

%% Right way: Initialization updates internal Gain
maskObj.Initialization = 'set_param([gcb ''/Gain''], ''Gain'', GainValue);';
📊

Quick Reference

StepActionDescription
1Create SubsystemSelect blocks and group into a subsystem
2Create MaskRight-click subsystem > Mask > Create Mask
3Add ParametersDefine inputs users can set
4Write InitializationLink parameters to internal blocks
5Customize IconDraw icon using commands
6Apply and SaveClose editor to apply mask

Key Takeaways

Create a subsystem first before applying a mask in Simulink.
Use the Mask Editor to add parameters, customize icons, and write initialization code.
Initialization code is essential to connect mask parameters to internal block settings.
Only subsystems can be masked, not individual blocks.
Test the mask by changing parameters and verifying internal block behavior.