Validating Mask Parameters Using Constraints

A mask can contain parameters that accept user input values. You can provide input values for mask parameters using the mask dialog box. Mask parameter constraints help you to create validations on a mask parameter without having to write your own validation code. Constraints ensure that the input for the mask parameter is within a specified range. For example, consider a masked Gain block. You can set a constraint where the input value must be between 1 and 10. If you provide an input that is outside the specified range, an error displays.

Create and Associate a Constraint

Launch Constraint Manager

Mask Editor contains a Constraint Manager with attributes and options to create your constraints. You can launch the Constraint Manager in two ways:

  • Click the Constraint Manager button in Mask Editor

  • While editing a parameter, select Add New Constraint from the Constraint drop-down menu under Property Editor.

Create a Constraint

You can create constraints according to your specification using the built-in attributes in the Constraint Manager. To create a constraint:

  1. In the Constraint Manager, click Create Constraint.

  2. Select attributes for the constraint in the Rule section. Depending on the data type selected the rule attributes change.

    For more details on rule attributes, see Rule Attributes in Constraint Manager.

  3. Click Apply to create the constraint.

Associate the Constraint to a Mask Parameter

Once a constraint is created, you can associate it with any Edit or Combobox parameters in the Mask Editor.

  1. In the Mask Editor, select the parameter you want to associate a constraint with.

  2. Select the constraint name from the Constraint drop-down menu.

  3. Click Apply to associate the constraint.

Validate the Constraint

To check if the parameter is in adherence with the associated constraint:

  1. Select a parameter with a constraint associated with it.

  2. Provide the input values for the parameter in the Property editor. If the input is outside the specification for the associated constraint, an error displays.

Create a Cross-Parameter Constraint

Cross-parameter constraints are applied among two or more Edit or Combobox type mask parameters. You can use a cross parameter constraint when you want to specify scenarios such as, Parameter1 must be greater than Parameter2.

  1. Launch Constraint Manager.

  2. Click the Cross-Parameter Constraints tab.

  3. Click Create Constraint. A new cross-parameter constraint is created with a default name (Constraint_1). You can change the constraint name.

  4. Specify the following values for the new constraint:

    • Name – Specify a name for the constraint

    • MATLAB Expression – Specify a valid MATLAB expression. This expression is evaluated during edit time and simulation

    • Error Message – Specify the error message to be displayed when the constraint rule is not met. If no error message is specified, a default error message displays.

  5. Click Apply.

The Constraint Manager also helps you to create:

  • Custom Constraints – if the built-in attributes do not correspond with your needs.

  • Shared Constraints – where the constraint is saved in a MAT file and can be shared with multiple block masks.

Related Topics