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 an 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. You can also associate a literal edit parameter to a constraint. The constraint is either a rule specified with the available attributes or is a MATLAB expression.
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.
open_system("slexMaskConstraints.slx");
Create and Associate Custom Constraint to Mask Parameter
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.
a. Enter the name of the constraint as evenNumberConstraint
.
b. 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.
c. 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.
Define and Associate Constraint to Mask Literal Edit Parameter
You can associate constraints to literal edit parameters. If the Evaluate option is not selected, Simulink® takes a literal reading of the input entry as you type it in the mask parameter dialog box. For example, you can store an IP address in a literal edit parameter and validate it against a custom rule.
In the example model, refer to the Define and Associate Constraint to Mask Literal Edit Parameter section.
To create a custom constraint:
1. Create a mask on the subsystem block.
2. Create an edit parameter IPAddress
. Clear the Evaluate option in the Attributes section to store the literal value of the parameter IPAddress
.
3. In the Mask Editor, select Constraints > Parameter.
a. Enter the name of the constraint as IPAddressConstraint
.
b. Specify the MATLAB expression as slexMaskIsValidIPAddress(value)
.
The function slexMaskIsValidIPAddress
stored in slexMaskIsValidIPAddress.m
file checks if IPAddress
is a valid IP address. The function returns true
if IPAddress
is a valid IP address and returns false
otherwise.
function out = slexMaskIsValidIPAddress(addr) out = true; if ~isempty(regexp(strtrim(addr), '^\d{1,3}(\.\d{1,3}){3}$', 'once')) ipVec = sscanf(addr, ['%u' '.' '%u' '.' '%u' '.' '%u']); if ~all(ipVec <= 255) out = false; end else out = false; end end
Associate Constraint to Mask Literal Edit Parameter
1. In the Mask Editor, click the Parameters & Dialog tab.
2. Click the mask parameter.
3. In the Property Editor pane in the Attributes section, select the constraint IPAddressConstraint
from Constraint.
Validate the Constraint
To check if the parameter adheres to the constraint:
1. Double-click the Constraint on literal edit parameter
Subsystem block.
2. Enter an invalid IP address in the mask dialog box and click OK.
3. This error message appears.