Control Masks Programmatically

Simulink® defines a set of parameters that help in setting and editing masks. To set and edit mask from the MATLAB® command line, you can use instances of the Simulink.Mask and Simulink.MaskParameter classes. You can also use the get_param and set_param functions to set and edit masks. However, since these functions use de-limiters that do not support Unicode® (Non English) characters you must use instances of the Simulink.Mask and Simulink.MaskParameter classes to control masks.

Use Simulink.Mask and Simulink.MaskParameter

Use instances of Simulink.Mask and Simulink.MaskParameter classes to perform the following mask operations:

  • Create, copy, and delete masks

  • Create, edit, and delete mask parameters

  • Determine the block that owns the mask

  • Get workspace variables defined for a mask

  1. In this example the Simulink.Mask.create method is used to create a block mask:

     maskObj = Simulink.Mask.create(gcb);
     maskObj =  
          Simulink.Mask handle
          Package: Simulink
          Properties:
                        Type: ''
                 Description: ''
                        Help: ''
              Initialization: ''
              SelfModifiable: 'off'
                     Display: ''
                   IconFrame: 'on'
                  IconOpaque: 'on'
        RunInitForIconRedraw: 'off'
                  IconRotate: 'none'
                  PortRotate: 'default'
                   IconUnits: 'autoscale'
                  Parameters: []
      Methods, Events, Superclasses
  2. In this example the mask object is assigned to variable maskObj using the Simulink.Mask.get method:

    maskObj = Simulink.Mask.get(gcb)
    maskObj =  
        Simulink.Mask handle
        Package: Simulink
        Properties:
                        Type: ''
                 Description: ''
                        Help: ''
              Initialization: ''
              SelfModifiable: 'off'
                     Display: ''
                   IconFrame: 'on'
                  IconOpaque: 'on'
        RunInitForIconRedraw: 'off'
                  IconRotate: 'none'
                  PortRotate: 'default'
                   IconUnits: 'autoscale'
                  Parameters: [1x1 Simulink.MaskParameter]
         Methods, Events, Superclasses

For examples of other mask operations, like creating and editing mask parameters and copying and deleting masks see Simulink.Mask and Simulink.MaskParameter .

Use get_param and set_param

The set_param and get_param functions have parameters for setting and controlling the mask. You can use these functions to set the mask of any block in the model or library based on a value passed from the MATLAB command line:

set_param(gcb,'MaskStyleString','edit,edit',..
'MaskVariables','maskparameter1=@1;maskparameter2=&2;',..
'MaskPromptString','Mask Parameter 1:|Mask Parameter 2:',..
'MaskValues',{'1','2'});
get_param(gcb,'MaskStyleString');
set_param(gcb,'MaskStyles',{'edit','edit'},'MaskVariables',..
'maskparameter1=@1;maskparameter2=&2;','MaskPrompts',..
{'Mask Parameter 1:','Mask Parameter 2:'},..
'MaskValueString','1|2'); 
get_param(gcb,'MaskStyles');

where

  • | separates individual string values for the mask parameters

  • @ indicates that the parameter field is evaluated

  • & indicates that the parameter field is not evaluated but assigned as a string

    Note:   When you use get_param to get the Value of a mask parameter, Simulink returns the value that was last applied using the mask dialog. Values that you have entered into the mask dialog but not applied will not be reflected when you use the get_param command.

See Mask Parameters for detailed information on the mask parameters.

Programmatically Create Mask Parameters and Dialogs

This example shows how to create this simple mask dialog, add controls to the dialog, and change the properties of the controls.

  1. Create the mask for a block you selected in the model.

    maskobj = Simulink.Mask.create(gcb);
  2. Create parameters for the mask. You can change the location of these parameters on the dialog in a subsequent step.

    maskobj.addParameter('Type',  'checkbox',  'Prompt',  'Option 1',  'Name',  'option1');
    maskobj.addParameter('Type',  'checkbox', 'Prompt',  'Option 2',  'Name',  'option2');
    

    These commands create a mask dialog with the default layout. Simulink adds the two parameters to the Parameters group, which is internally named as ParameterGroupVar by default.

    You can verify the group name programmatically.

    dlg = maskobj.getDialogControls();
    dlg(2)
    ans =
    		Group with properties:
    					Name: 'ParameterGroupVar'
    					Prompt: 'Simulink:studio:ToolBarParametersMenu'
    					Row: 'new'
    					Enabled: 'on'
    					Visible: 'on'
    				DialogControls: [1x1 Simulink.dialog.parameter.CheckBox]
    
  3. To customize the dialog and to use tabs instead of the default group, remove the Parameters group box.

    maskobj.removeDialogControl('ParameterGroupVar');

    Simulink preserves the child dialog controls, the two check boxes in this example, even when you delete the ParametersGroupVar group surrounding them. This is because these controls are parameters, which cannot be deleted using the dialog control methods.

    You can delete parameters using methods such as Simulink.Mask.removeAllParameters, which belongs to the Simulink.Mask class.

  4. In order to create tabs, first create a tab container and get its handle.

    tabgroup = maskobj.addDialogControl('tabcontainer','tabgroup');
  5. Create tabs within this tab container.

    tab1 = tabgroup.addDialogControl('tab','tab1');
    tab1.Prompt = 'First';
    tab2 = tabgroup.addDialogControl('tab', 'tab2');
    tab2.Prompt = 'Second';
    tab3 = tabgroup.addDialogControl('tab','tab3');
    tab3.Prompt = 'Third (invisible)';

    Make the third tab invisible.

    tab3.Visible = 'off'
    
    tab3 = 
    
      Tab with properties:
    
                  Name: 'tab3'
                Prompt: 'Third (invisible)'
               Enabled: 'on'
               Visible: 'on'
        DialogControls: []
  6. Move the mask parameters you created previously to the first tab.

    opt1 = maskobj.getDialogControl('option1');
    opt2 = maskobj.getDialogControl('option2');
    
    opt1.moveTo(tab1);
    opt2.moveTo(tab1);
    

For more information on dialog controls and their properties, type Simulink.dialog.Control at the MATLAB command line.

Was this topic helpful?