Custom Constraints and Shared Constraints

The Constraint Manager helps you to create your own customized constraints using MATLAB code and also allows you to save constraints in separate MAT files so that you can use it with multiple block masks at the same time.

Create Custom Constraints

If the constraint you need cannot be created with the built-in attributes, you can create your custom constraint by writing your own MATLAB expression. To create a custom constraint:

  1. Launch Constraint Manager.

  2. Click Create Constraint.

  3. Enter a valid MATLAB expression in the MATLAB Expression field, under the Custom Constraint section.

  4. Write the error message for your custom constraint in the Error Message field

  5. Click Apply.

Create Shared Constraints

You can create and save parameter constraints in a MAT file. You can save a constraint in a MAT file either from the Mask Editor or programmatically. Multiple block masks can share a parameter constraint saved in a MAT file. To create a shared constraint:

  1. Launch Constraint Manager.

  2. Click Create Constraint.

  3. Create a constraint with the required attributes.

  4. Select the Save constraint to MAT file check box and specify the MAT file name.

  5. Click Apply.

A shared constraint is listed in the Property editor section in the format <MATFileName>:<ConstraintName>. You can select any available constraint as required and associate it with an Edit parameter.

While creating shared constraints it is recommended that you:

  • Save constraints to MAT files that are in the MATLAB path.

  • Provide meaningful names to constraints so that other users can understand the nature of the constraint easily.

Constraints associated to an Edit parameter from a MAT file (shared constraints) are validated during edit-time and simulation.

Save and Load Constraints Programmatically

You can save constraints in a MAT file and load constraints from a MAT file programmatically.

To save a constraint in a MAT file:

uint16Constraint = Simulink.Mask.Constraints;
uint16Constraint.Name = 'uint16Constraint';
uint16Constraint.addParameterConstraintRule('DataType', 'uint16');
save('constraintList.mat', 'uint16Constraint','-append');  % appends 'uint16Constraint'
save('constraintList.mat', 'uint16Constraint') % overwrites the MAT file with 'uint16Constraint'

Note

It is recommended that the constraint name (for example, uint16Constraint.Name = 'uint16Constraint') and the variable used for storing the constraint (for example, uint16Constraint = Simulink.Mask.Constraints) have the same name.

To load a constraint from a MAT file:

myConstraints = load(‘constraintList’) % loads constraints into the variable 'myConstraints'

Rule Attributes in Constraint Manager

Rule attributes available in the Constraint Manager to create constraints are shown in the table:

Rule AttributeAttribute ValueDescription

Data type

double, single, numeric, integer, int8, uint8, int16, uint16, int32, uint32, int64, uint64, boolean, enum, fixdt, string

Specify the acceptable data type of the mask parameter value. For example, if the data type specified is uint8, the acceptable value is in the range of uint8, that is, 0–255.

Complexity

real, complex

Specify if the mask parameter value can be a real or complex number.

Dimensions

scalar, row vector, column vector, 2-D matrix, n-D matrix

Specify the acceptable dimensions for the mask parameter.

Sign

positive, negative, zero

Specify if the input value can be positive, negative, or zero.

Finiteness

finite, inf, -inf, NaN

Specify the acceptable finiteness of mask parameter value.

Range

Minimum, Maximum

Specify the acceptable range of mask parameter value.

Custom Constraint

Valid MATLAB expression

Specify custom constraint for the mask parameter using a valid MATLAB expression. You can use the value token to parameterize the expression. 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, the MATLAB expression evaluates as Parameter1 > 100.

Custom Error Message

Character vector

Specify a custom error message for the custom constraint. You can specify the error message as character vector or as a message catalog ID.

Related Topics