Main Content

addConstraint

Add constraint to variant configuration data object

    Description

    Add-On Required: This feature requires the Variant Manager for Simulink add-on.

    addConstraint(varConfigData,ConstraintName=constraintName) adds a constraint with the name constraintName to a Simulink.VariantConfigurationData object varConfigData.

    You can use constraints to check if all the variant configurations present in varConfigData satisfy certain conditions. The constraint must be specified as a valid Boolean condition expression. The expression can contain variant control variables used in the model hierarchy, names of variant configurations of referenced components in the hierarchy, or a combination of these (since R2025a). Simulink® evaluates these constraints during variant configuration activation, model compilation, simulation, and code generation workflows.

    example

    addConstraint(___,Name=Value) specifies options using one or more name-value arguments in addition to the input arguments in previous syntaxes, for example, option to set the condition expression for the new constraint.

    Examples

    collapse all

    Add a new constraint to the variant configuration data object associated with a top-level model. Define the condition using variant control variables used in the model hierarchy.

    modelName="slexVariantManagement";
    open_system(modelName);
    vcd = Simulink.VariantManager.getConfigurationData("slexVariantManagement");
    
    addConstraint(vcd,ConstraintName="ControllerPlantConstraint", ...
     Condition="~((Ctrl == ControllerType.Linear) && (PlantLoc ==  PlantLocation.External))", ...
     Description="Linear controller must not have an external plant")
    

    Get the variant configuration data object for a referenced component in the model hierarchy.

    open_system("slexVariantManagementExternalPlantMdlRef");
    refvcd = Simulink.VariantManager.getConfigurationData("slexVariantManagementExternalPlantMdlRef");

    Add another constraint to the top-level model using names of configurations defined for the referenced component.

    addConstraint(vcd,ConstraintName="ExternalPantFidelityConstraint", ...
     Condition="(isConfigActive(refvcd,""LowFid"") || isConfigActive(refvcd,""HighFid""))", ...
     Description="External plant fidelity must be set to low or high")

    Input Arguments

    collapse all

    Variant configuration data to which you want to add a new constraint, specified as a Simulink.VariantConfigurationData object.

    Name of the constraint, specified as a character vector or string scalar.

    Example: "LinInternal"

    Data Types: char | string

    Name-Value Arguments

    collapse all

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: addConstraint(vcd,ConstraintName="ControllerPlantConstraint", ... Condition="~((Ctrl == ControllerType.Linear) && (PlantLoc == PlantLocation.External))", ... Description="Linear controller must not have an external plant")

    Boolean condition expression that is defined in terms of variant control variables and referenced component configurations, specified as a character vector or string scalar. The expression must evaluate to true to satisfy the constraint.

    Example: "~((Ctrl==ControllerType.Linear) && (PlantLoc==PlantLocation.External))"

    Data Types: char | string

    Description of the constraint, specified as a character vector or string scalar.

    Example: "Linear controller must not have an external plant"

    Data Types: char | string

    Limitations

    • Variant Manager constraints are evaluated based on the values of variant control variables defined in the base workspace or data dictionaries linked to the model for which the constraint is defined.

    Version History

    Introduced in R2013b

    expand all