MATLAB For Beginners: 20-Minute Video Training Course. (Grab The Training Course and The Cheat Sheet for FREE)

       

After reading this article you’ll know:

  • When you should use a mask
  • How to create and document a mask on a block 

Why Use a Mask ?

Mainly, masks are used to document a particular block to explain what it does. This is particularly useful for library blocks since one of the incentives is saving time by using a block that has already been coded. To do so, it should not be necessary to understand how the block was coded; this is why documenting a block in this way can be very useful. 

Adding a mask can also be used to prevent a MATLAB script from entering into a particular block. For example, if you want to apply a script that checks the naming rules related to the parameters of your model, you may want your script not to enter into your mask’s blocks. Using a mask allows you to define parameters locally to a specific block. Thus it could make sense to have different naming rules for a block with a mask since there is no risk of conflict with parameters defined somewhere else in the model.  

Create and Document a Mask

Let start with the model “mySimpleModel.slx”:

Simulink Model Generated from Matlab

 

If you want to have this model, you can run the script at the end of the article. We will create a mask on the subsystem. To do so:

  • Step 1: Right click on the subsystem > Mask > Create Mask …
  • Step 2: The following window will open:

  • Step 3: Go to Documentation if you want to add a description to your subsystem.

If you click “Apply” and double click the block “Subsystem” instead of entering inside the block, you will merely see the information you just entered in the “Documentation” tab of your mask.

Script to create the model “mySimpleModel.slx”:

modelName = 'mySimpleModel';
 
% create and open the new test model
open_system(new_system(modelName));
 
offset = 100;
hSubsystems = 60;
 
% position Sine Wave
xSW= 30;
ySW = 30;
wSW = 30;
hSW = 30;
posSW= [xSW ySW xSW+wSW ySW+hSW];
 
% add the Sine Wave from Simulink library
blockName = 'Sine Wave';
add_block(['simulink/Sources/' blockName], [modelName '/' blockName], 'Position', posSW);
 
% create a subsytem with I/O
add_block('simulink/Sources/In1', [modelName '/In1']);
add_block('simulink/Sinks/Out1', [modelName '/Out1']);
inputPort = get_param([modelName '/In1'], 'handle');
outputPort = get_param([modelName '/Out1'], 'handle');
subSystemContent = [inputPort outputPort];
Simulink.BlockDiagram.createSubSystem(subSystemContent);
 
% delete the I/O created in modelName 
delete_line(modelName,'In1/1','Subsystem/1');
delete_line(modelName,'Subsystem/1','Out1/1');
delete_block([modelName '/In1']);
delete_block([modelName '/Out1']);
 
% add line between the Input and Output ports
add_line(modelName, 'Sine Wave/1', 'Subsystem/1', 'autorouting', 'on');
 
% position Subsystem
wSubsystem = 8;
posSubsystem = posSW; 
posSubsystem(1) = posSubsystem(1)+offset;
posSubsystem(2) = posSW(2)-(hSubsystems-hSW)/2;
posSubsystem(3) = posSubsystem(3)+offset+wSubsystem;
posSubsystem(4) = posSubsystem(2)+hSubsystems;
set_param([modelName '/Subsystem'],'Position', posSubsystem);
 
% position inport
pathInport = [modelName '/Subsystem/In1'];
currentPos = get_param(pathInport, 'Position');
currentHeight = currentPos(4) - currentPos(2);
currentWidth = currentPos(3) - currentPos(1);
posInport(1) = posSW(1);
posInport(2) = posSW(2);
posInport(3) = posInport(1)+currentWidth;
posInport(4) = posInport(2)+currentHeight;
set_param(pathInport,'Position', posInport);
 
% add the Add block from Simulink library
blockName = 'Add';
pathAdd = [modelName '/Subsystem/' blockName];
add_block(['simulink/Math Operations/' blockName], pathAdd);
currentPos = get_param(pathAdd, 'Position');
currentHeight = currentPos(4) - currentPos(2);
currentWidth= currentPos(3) - currentPos(1);
posAdd(1) = posInport(1) + offset;
posAdd(2) = posInport(2)-1;
posAdd(3) = posAdd(1)+currentWidth;
posAdd(4) = posAdd(2)+currentHeight;
set_param(pathAdd,'Position', posAdd);
 
% add line between the Input and Output ports
add_line([modelName '/Subsystem'], 'In1/1', 'Add/1', 'autorouting', 'on');
 
blockName = 'Constant';
pathConstant = [modelName '/Subsystem/' blockName];
add_block(['simulink/Sources/' blockName], pathConstant);
currentPos = get_param(pathConstant, 'Position');
currentHeight = currentPos(4) - currentPos(2);
currentWidth = currentPos(3) - currentPos(1);
posConstant(1) = posSW(1);
posConstant(2) = posSW(2)+offset/2;
posConstant(3) = posConstant(1)+currentWidth;
posConstant(4) = posConstant(2)+currentHeight;
set_param(pathConstant,'Position', posConstant);
 
% add line between the Input and Output ports
add_line([modelName '/Subsystem'], [blockName '/1'], 'Add/2', 'autorouting', 'on');
 
% position outport
blockName = 'Out1';
pathOutport = [modelName '/Subsystem/' blockName];
currentPos = get_param(pathOutport, 'Position');
currentHeight = currentPos(4) - currentPos(2);
currentWidth = currentPos(3) - currentPos(1);
posOutport(1) = posAdd(1) + offset;
posOutport(2) = posInport(2) + (posAdd(4) - posAdd(2))/2 - currentHeight/2;
posOutport(3) = posOutport(1)+currentWidth;
posOutport(4) = posOutport(2)+currentHeight;
set_param(pathOutport,'Position', posOutport);
 
% add line between the Input and Output ports
add_line([modelName '/Subsystem'], 'Add/1', [blockName '/1'], 'autorouting', 'on');
 
 
blockName = 'Scope';
pathScope = [modelName '/' blockName];
add_block(['simulink/Sinks/' blockName], pathScope);
currentPos = get_param(pathScope, 'Position');
currentHeight = currentPos(4) - currentPos(2);
currentWidth = currentPos(3) - currentPos(1);
posScope(1) = posSubsystem(1) + offset;
posScope(2) = posSubsystem(2) + (posSubsystem(4) - posSubsystem(2))/2 - currentHeight/2;
posScope(3) = posScope(1)+currentWidth;
posScope(4) = posScope(2)+currentHeight;
set_param(pathScope,'Position', posScope);
 
% add line between the Input and Output ports
add_line(modelName, 'Subsystem/1', [blockName '/1'], 'autorouting', 'on');

Leave a Reply