Validate Mask Parameters or Port Signals Using Constraints
You can use parameter constraints to specify the range of values or data types for a mask parameter, allowing you to validate mask parameters on a masked block without having to write your own validation code. For example, you can set a constraint so that the input value for a Gain block must be between 1 and 10. If you provide an input that is outside the specified range, an error is displayed.
Similarly, you can use port constraints to validate compile-time signal attributes on the input and output ports of a masked block. Compile-time signal attributes include data type, complexity, and dimensions. You can define the rules to validate the data types, complexity (real or complex), and dimensions (scalar, vector, or 2-D matrix) of port signals. You can also reuse the constraints by creating and saving them in an XML file and sharing them across masks of different blocks in the same model or different models.
Use constraints to:
Validate parameter values and signals consistently.
Display consistent error messages for validation failures.
Eliminate duplicate validation code.
Create Parameter or Port Constraint
To create a new constraint, in the Mask Editor, click the Constraints tab.

The Constraint Gallery has these constraints:
Parameter — Use parameter constraints to define the acceptable range of values for the mask parameter. For more information, see Create and Validate Mask Parameters Using Constraints.
Port — Use port constraints to define the acceptable signal attributes for an input or output port. For more information, see Use Port Constraints to Validate Input and Output Signals.
Cross Parameter — Use cross parameter constraints to define rules that involve multiple mask parameters.
Cross Port — Validate compile-time signal attributes across ports within the same masked block. For more information, see Validate Port Signals Among Ports of the Same Masked Block.
Cross Port Parameter — Use cross port parameter constraints to define rules involving both ports and parameters within the same masked block. For more information, see Validate Port Signals and Parameter Values Using Cross-Port Parameter Constraints.
You can define constraints by using one of these methods:
Select Data Types — In the Select Data Types section, you can specify the allowed data types and numeric properties for a parameter or port signal. You can choose from supported data types such as
numeric,boolean,string, orenum, and configure numeric properties including Data Types, Complexity, and Dimension.Custom Rule — Use custom rules to define parameter constraints in the form of MATLAB expressions that return a logical value. You can use the
valuetoken to parameterize the expression, which helps in assigning a constraint to multiple parameters. During validation, the evaluated value of the parameter replaces thevaluetoken. For example, if the MATLAB expression for a constraint isvalue > 100and is associated with the edit type mask parameter,Parameter1, then the MATLAB expression evaluates asParameter1 > 100.Commonly Used Constraints — Choose a predefined constraint from the Shared Constraints section in the Constraint Browser to associate it with a mask parameter instead of creating it manually. These constraints are available only for the parameter constraint type.
Associate Constraint With Parameters or Ports
After you create a constraint, you associate it with mask parameters for it to take effect. In the Constraints tab, in the Associations pane, select the mask parameters from the Parameters list to associate the constraint with parameters.
For port constraints, you must define a port identifier for each port of the masked block. A port identifier helps you to identify a port within a masked block either by name or by numerical index. Select the port identifier from the Select Ports list to associate the port constraint with the port.
You can also set a parameter condition while defining port constraints. The port constraint becomes active on the associated port only when all the specified parameter conditions are met.
Execute Constraints
Constraint validation occurs when you:
Shift the focus from the parameter widget or click Apply in the mask dialog box.
Modify the value of a parameter using the
set_paramfunction.Update the diagram, simulate the model, or generate code.