Main Content

Custom Constraint for Mask Parameter

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 input value for the mask parameter satisfies the rules associated with the constraints. For example, consider a masked Subsystem block. You can set a constraint where the input value must be even number. If you provide an input that is not an even number, an error is displayed. If the rule you want specify is not present as one of the attributes, then specify a MATLAB® expression that returns a scalar.

Explore Model

The example model contains a subsystem block whose block parameters evenNoParam1 and evenNoParam2 must be an even number. You have custom the constraint using MATLAB expression as the rule you want to specify is not present as one of the attributes.


Create Custom Constraint

In the example model, refer to the Custom Constraint section.

To create a custom constraint:

1. Create a mask on the subsystem block.

2. In the Mask Editor, select Constraints > Parameter.

i. Enter the name of the constraint as evenNumberConstraint.

ii. Enter the MATLAB expression as mod(value,2)==0.

Note: You can use the value token to parameterize the expression, which helps in assigning a constraint to multiple parameters. During validation, the evaluated value of the parameter replaces the value token. For example, if the MATLAB expression for a constraint is value > 100 and is associated with the edit type mask parameter, Parameter1, then the MATLAB expression evaluates as Parameter1 > 100. This helps in assigning the constraint to multiple parameters.

iii. In Error Message, enter The value of the parameter must be even number.. Save the mask.

Associate Custom Constraint to Mask Parameters

You must associate constraints to mask parameters. You can associate the same constraint among multiple mask parameters.

1. In the Mask Editor, click the Parameters & Dialog tab.

2. Click the mask parameter.

3. In the Property Editor, go to the Attributes section, and select the constraint from the Constraint.

Note: You can associate the same constraints with multiple mask parameters. The Constraints Browser helps you to manage constraints. If you want to create a constraint that involves multiple parameters, use cross-parameter constraints instead of custom constraints.

Validate Constraint

To check if a parameter adheres to the associated constraint:

1. Go to the Mask Editor and select the parameter.

2. In the Property Editor, provide input values that are outside of the range specified in the associated constraint. An error displays.

Related Topics