Main Content

Share Parameter Constraints Across Multiple Block Masks

This example shows how to create a parameter constraint and share it across masks of different blocks in the same model or different models. You can save the parameter constraint in a XML file and associate the constraint across multiple block masks. Defining a shared constraint in an XML file enables you to edit the data and compare the file with a previous version.

A mask can contain parameters that accept user input values. You can provide input values for mask parameters using the mask dialog box. Mask parameter constraints enable you to create validations on a mask parameter without having to write your own validation code. Constraints ensure that the input value for the mask parameter satisfies the rules associated with the constraints. For example, consider a masked Subsystem block. You can set a constraint where the input value must be between 1 and 10. When you provide an input that is outside the specified range, the software displays an error. For information on mask parameter constraints, see Validating Mask Parameters Using Constraints.

Note: To use shared constraint created in a MAT file, you must convert it to an XML file using the syntax Simulink.Mask.Constraints.convertMatToXML(matFileName, xmlFileName). matFileName is the name of the MAT file to convert and xmlFileName is the name of the XML file to create. See Simulink.Mask.Constraints.convertMatToXML for more details.

Explore the Model

This model has two subsystem blocks named SharedConstraintBlock1 and SharedConstraintBlock2. Each block parameter value on each subsystem block must be a real scalar. Instead of creating separate constraints for these blocks, you can create a single constraint in an XML file and share it across the two subsystem blocks.


Create Shared Constraint

In the example model, refer the shared constraint blocks SharedConstraintBlock1 and SharedConstraintBlock2.

Create a shared constraint:

1. Create a mask on the SharedConstraintBlock1 block.

2. Go to Mask Editor, on the Constraints tab, click new and enter the name of the XML file, sharedConstraint.

3. Create the parameter constraint in the XML file. Click Parameter Constraint and specify the attributes for the constraint. The Constraint Browser helps you to manage shared constraint.

4. On the Parameters & Dialog tab, click the parameter.

5. Select Property Editor > Attributes > Constraint and select the XML file constraint.

Associate Constraint Across Multiple Blocks or Models

Reuse the constraint sharedConstraint:realScalarPositiveConstraint in the SharedConstraintBlock2 block.

1. Create a mask on the SharedConstraintBlock2 block.

2. Select Mask Editor > Constraint > Load to load the constraint. Select the XML file and save the mask.

3. Go to the Parameters & Dialog tab and click the parameter.

4. Select Property Editor > Attributes > Constraints and select the XML file.

Validate the Constraint

Provide an invalid value to the parameter using mask dialog. Observe that an error dialog is displayed on Apply.

See Also