Search code examples
matlabsimulinkmasking

Simulink: Control Variant Subsystems Using Mask


I would like to find a simple process for switching the model innards under a mask using the mask parameters.

This question has expanded enough such that it has been reimplemented here.

Variant subsystems are an excellent method and can be controlled via workspace parameters;
however, I have found mask parameters to not interface with the variant subsystem selection.

This link is the first of a series of posts on how to use mask parameters to make changes to blocks inside of the system; however, the method is not as intuitive as using variant subsystems and a switch.
The link is also from 2008 and I believe that it may have been superceded at some point.

MWE

I have made a model containing a system labeled Source.
It is connected to a Display block which displays its output.

Source is a variant subsystem.
It contains 3 variants:

  • Source\One
  • Source\Two
  • Source\Three.

Each variant contains one Constant block.
The value of the Constant block is eponymous with the block label.
For example, Source\Two contains a constant block with value 2.

Source is also a masked subsystem.
Its mask contains a Radio Button parameter with a value labeled variantValue.
The Radio Button options for the variantValue parameter are:

  • Choice 1
  • Choice 2
  • Choice 3

The mask Initialization code is as follows:

switch variantValue

case 'Choice 1'
set_param('Source','OverrideUsingVariant','One')
disp('One')

case 'Choice 2'
set_param('Source','OverrideUsingVariant','Two')

case 'Choice 3'
set_param('Source','OverrideUsingVariant','Three')

end

I have set the variant to Override. I cannot set the mask to allow library blocks to modify contents, as this is greyed out.

I will drop the variant subsystem deeper into the hierarchy from the masked subsystem when a masked subsystem which is a variant subsystem works.


Solution

  • To do anything which does not go via your base workspace, you first need to set the "Overwrite variant conditions...", now you can choose the active variant with code:

    set_param('untitled/Variant Subsystem','OverrideUsingVariant','Variant1')
    

    What remains is creating a mask which, whenever the parameter is changed in your mask, runs the above line. This can be done via the initialisation commands.