Main Content

addConstraint

Class: Simulink.Mask.SharedConstraintFile
Namespace: Simulink.Mask

Add constraint to XML file

Since R2023a

Description

fileobj.addConstraint(constraint) adds a constraint in XML file format. Constraint is a Simulink.Mask.Constraints object or Simulink.Mask.PortConstraint that you must first create by using saveConstraints.

example

Input Arguments

expand all

Parameter or port constraint, specified as a Simulink.Mask.Constraints object or Simulink.Mask.PortConstraint object. Save the constraint using saveConstraints.

Data Types: cell

Examples

expand all

This example shows how to add parameter constraints and port constraints to an XML file and share it across masked blocks.

Add Parameter Constraint

1. Create a Simulink.Mask.Constraint object to create a parameter constraint.

    new_system('shareConstraint')
    add_block('built-in/subsystem','shareConstraint/subsystem');
    save_system;
    open_system('shareConstraint');
    constraintObj = Simulink.Mask.Constraints;

2. Assign a name to the constraint.

    constraintObj.Name = 'numericconst';

3. Add parameter constraint rules to the constraint.

    constraintObj.addParameterConstraintRule('DataTypes',{'numeric'},...
                                         'Fraction',{'integer'});

4. Create a Simulink.Mask.SharedConstraintFile object to add constraint to an XML file.

    fileobj = Simulink.Mask.SharedConstraintFile;
    fileobj.FileName = "sharedconstraint";
    fileobj.addConstraint(constraintObj)
ans = 
  Constraints with properties:

               Name: 'numericconst'
    ConstraintRules: [1x1 Simulink.Mask.ParameterConstraintRules]

Add Port Constraint

1. Create a Simulink.Mask.PortConstraint object to create a port constraint.

    portConstraintObj = Simulink.Mask.PortConstraint;

2. Assign a name to the constraint.

    portConstraintObj.Name='scalarInt8SharedConstraint';

3. Add port constraint rule.

    portConstraintObj.setRule('DataType',{'fixedpoint'},'Dimension',{'scalar'},'Complexity',{'real'});

4. Add port constraint to the XML file.

    fileobj.addConstraint(portConstraintObj)
ans = 
  PortConstraint with properties:

                   Name: 'scalarInt8SharedConstraint'
    ParameterConditions: [0x0 Simulink.Mask.ParameterCondition]
                   Rule: [1x1 Simulink.Mask.PortConstraintRule]
        DiagnosticLevel: 'error'
      DiagnosticMessage: ''

Version History

Introduced in R2023a