Hauptinhalt

addParameterConstraint

R2026b

Class: Simulink.Mask
Namespace: Simulink

Add parameter constraint to mask

Description

paramConstraint = maskObj.addParameterConstraint(ParamConstraintObj) adds a parameter constraint to the specified mask by using a Simulink.Mask.Constraints object. You can only associate parameter constraints with Edit and Combo Box type mask parameters.

paramConstraint = maskObj.addParameterConstraint(Name,Value) adds a parameter constraint with options specified by name-value arguments.

example

Input Arguments

expand all

Block mask handle, specified as a Simulink.Mask object. You can use the Simulink.Mask.get command to get the block mask handle. For more information, see Simulink.Mask.get.

Parameter constraint object, specified as a Simulink.Mask.Constraints object.

Name-Value Arguments

expand all

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. You can specify several name and value arguments in any order as Name1,Value1,...,NameN,ValueN.

Name of the mask constraint, specified as a character vector or string. This value must be a valid MATLAB® name. This argument is required.

Data Types: char | string

Name of the mask parameter to which you want to associate the constraint, specified as a cell array of character vectors or a string. This argument is optional.

Data Types: cell | string

Rules for mask parameter constraint, specified as a cell array of the options below. A constraint can only have a single rule. This argument is required.

Name-Value Arguments for Rules

Name

Value

DataTypesnumeric, double, single, int8, uint8, int16, uint16, int32, uint32, int64, uint64, half, string, boolean, enum
Dimensionscalar, row vector, column vector, 2-D matrix, n-D matrix
Complexityreal, complex
Signpositive, negative, zero
Finitenessfinite, inf, -inf, NaN
Fractioninteger, decimal
MinimumCharacter vector
MaximumCharacter vector
CustomConstraint

Valid MATLAB expression returning logical true or false.

CustomErrorMessageCharacter vector or a message catalog ID. If you use the message catalog ID to specify an error, the error message must not have any holes in it. This error message is specifically for custom constraint validation failure.

Example: Rules = {'DataTypes',{'double'}, 'Dimension',{'scalar'}, 'Complexity',{'real'}, 'Sign',{'positive'}, 'Finiteness',{'finite'}, 'Minimum','0.1', 'Maximum','1'}

Data Types: cell

Output Arguments

expand all

Handle to the mask parameter constraint, returned as a Simulink.Mask.Constraints object. You can associate a constraint with the mask parameter either during or after creating the constraint.

Examples

expand all

Create a model and add a subsystem block.

open_system(new_system("addParamConstExample"));
add_block("built-in/subsystem","addParamConstExample/subsystem");

Create a mask object for the subsystem.

maskObj = Simulink.Mask.create(gcb);

Create Edit type mask parameters named gain1 and gain2.

maskObj.addParameter(Type="edit", Name="gain1", Value="0.5");
maskObj.addParameter(Type="edit", Name="gain2", Value="2");

Associate a parameter constraint named gainConstraint with the parameter gain1.

GainConstraint1 = maskObj.addParameterConstraint(Name="gainConstraint", Parameters={'gain1'},...
Rules={'DataTypes',{'double'}, 'Dimension',{'scalar'}, 'Complexity',{'real'},...
'Sign',{'positive'}, 'Finiteness',{'finite'}, 'Minimum','0.1', 'Maximum','1'});
GainConstraint1 = 

  Constraints with properties:

               Name: 'gainConstraint'
    ConstraintRules: [1×1 Simulink.Mask.ParameterConstraintRules]

Associate a custom constraint named EvenConstraint with the parameter gain2.

paramConstraint = Simulink.Mask.Constraints;
paramConstraint.Name = "EvenConstraint";
paramConstraint.addParameterConstraintRule('CustomConstraint', 'mod(value,2)==0');
GainConstraint2 = maskObj.addParameterConstraint(paramConstraint);
GainConstraint2 = 

  Constraints with properties:

               Name: 'EvenConstraint'
    ConstraintRules: [1×1 Simulink.Mask.ParameterConstraintRules]

Display the rules associated with the constraint EvenConstraint.

GainConstraint2.ConstraintRules
ans = 

  ParameterConstraintRules with properties:

             DataTypes: {'numeric'}
              Fraction: {0×1 cell}
             Dimension: {0×1 cell}
            Complexity: {0×1 cell}
                  Sign: {0×1 cell}
            Finiteness: {0×1 cell}
               Minimum: ''
               Maximum: ''
      CustomConstraint: 'mod(value,2)==0'
    CustomErrorMessage: ''

Simulate the model to evaluate the constraints.

Version History

Introduced in R2018a