Main Content

addConstraint

R2026b

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 model and add a subsystem block.

open_system(new_system('shareConstraintXML'))
add_block('built-in/subsystem','shareConstraintXML/subsystem');

2. Create a mask object for the subsystem.

maskObj = Simulink.Mask.create('shareConstraintXML/subsystem');

3. Create a mask parameter named p1 of type edit.

p1 = maskObj.addParameter('Name', 'p1', 'Type', 'edit');

4. Create and add a parameter constraint.

constraintObj = Simulink.Mask.Constraints;
constraintObj.Name = 'numericconst';
constraintObj.addParameterConstraintRule('DataTypes',{'numeric'},...
    'Fraction',{'integer'});

5. Create a shared constraint file and add the parameter constraint to the XML file.

fileobj = Simulink.Mask.SharedConstraintFile;
fileobj.FileName = 'SharedConstraintExample';
fileobj.addConstraint(constraintObj);
fileobj.saveConstraints();

6. Associate the mask parameter p1 to the constraint created in the shared XML file.

p1.ConstraintName = 'SharedConstraintExample:numericconst';
save_system;

Add Port Constraint

1. Create a port identifier named in1.

portId = maskObj.addPortIdentifier('Name','in1','Type','Input');

2. Create and define a port constraint named scalarSharedConstraint.

portConstraintObj = Simulink.Mask.PortConstraint;
portConstraintObj.Name = 'scalarSharedConstraint';
portConstraintObj.setRule( ...
    'DataType', {'fixedpoint'}, ...
    'Dimension', {'scalar'}, ...
    'Complexity', {'real'} );

3. Add the port constraint to the XML file.

fileobj.addConstraint(portConstraintObj);
fileobj.saveConstraints();

4. Associate the port in1 to the port constraint created in the shared XML file.

maskObj.addPortConstraintAssociation('SharedConstraintExample:scalarSharedConstraint',{'in1'});
save_system;

Version History

Introduced in R2023a