Documentation

Simulink.Variant class

Package: Simulink

Specify conditions that control variant selection

Description

An object of the Simulink.Variant class represents a conditional expression called a variant control. The object allows you to specify a Boolean expression that activates a specific variant choice when it evaluates to true.

A variant control comprises one or more variant control variables, specified using MATLAB® variables or Simulink.Parameter objects.

You specify variant controls for each variant choice represented in a Variant Subsystem or Model Variant block. For a given Variant Subsystem or Model Variant block, only one variant control can evaluate to true at a time. When a variant control evaluates to true, Simulink® activates the variant choice that corresponds to that variant control.

Construction

variantControl = Simulink.Variant(conditionExpression) creates a variant control.

Properties

expand all

conditionExpressionVariant condition expression'' (default) | string

Variant condition expression, specified as a string containing one or more of these operands and operators.

Operands

  • Variable names that resolve to MATLAB variables or Simulink.Parameter objects with integer or enumerated data type and scalar literal values

  • Variable names that resolve to Simulink.Variant objects

  • Scalar literal values that represent integer or enumerated values

Operators

  • Parentheses for grouping

  • Arithmetic, relational, logical, or bit-wise operators

The variant condition expression evaluates to a Boolean value. This property has read and write access.

Example: '(Fuel==2 || Emission==1) && Ratio==2'

Examples

Create Variant Controls Using MATLAB Variables

Use MATLAB variables when you want to simulate the model but are not considering code generation.

Create MATLAB variables with scalar literal values.

Fuel = 3;
Emission = 1;
Ratio = 3;

Develop conditional expressions using the variables.

Variant1 = Simulink.Variant('Fuel==1 && Emission==2');
Variant2 = Simulink.Variant('(Fuel==2 || Emission==1) && Ratio==2');
Variant3 = Simulink.Variant('Fuel==3 || Ratio==4');

Create Variant Controls Using Simulink.Parameter Objects

If you want to generate preprocessor conditionals for code generation, use Simulink.Parameter objects instead of MATLAB variables.

Create variant Simulink.Parameter objects with scalar literal values.

Fuel = Simulink.Parameter(3);
Emission = Simulink.Parameter(1);
Ratio = Simulink.Parameter(3);

Specify the custom storage class for these objects as ImportedDefine so that the values are specified by an external header file.

Other valid values for the custom storage class are Define and CompilerFlag.

Fuel.CoderInfo.StorageClass = 'Custom';
Fuel.CoderInfo.CustomStorageClass = 'ImportedDefine';

Emission.CoderInfo.StorageClass = 'Custom';
Emission.CoderInfo.CustomStorageClass = 'ImportedDefine';

Ratio.CoderInfo.StorageClass = 'Custom';
Ratio.CoderInfo.CustomStorageClass = 'ImportedDefine';

Develop conditional expressions using the variables and create variant controls.

Variant1 = Simulink.Variant('Fuel==1 && Emission==2');
Variant2 = Simulink.Variant('(Fuel==2 || Emission==1) && Ratio==2');
Variant3 = Simulink.Variant('Fuel==3 || Ratio==4');

Related Examples

Was this topic helpful?