Main Content

Simulink.Variant class

Package:金宝app

Specify conditions that control variant selection

Description

An object of theSimulink.Variantclass 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 totrue.

A variant control comprises one or more variant control variables, specified using MATLAB®variables orSimulink.Parameter对象.

You specify variant controls for each variant choice represented in aVariant SubsystemorModel Variantblock. For a givenVariant SubsystemorModel Variantblock, only one variant control can evaluate totrueat a time. When a variant control evaluates totrue, Simulink®activates the variant choice that corresponds to that variant control.

Construction

variantControl= Simulink.Variant(conditionExpression)创建一个变量控制。

Properties

expand all

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

Operands

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

  • Variable names that resolve toSimulink.Variant对象

  • 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 UsingMATLABVariables

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 UsingSimulink.ParameterObjects

If you want to generate preprocessor conditionals for code generation, useSimulink.Parameter对象.

Create variantSimulink.Parameter对象with scalar literal values.

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

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

Other valid values for the custom storage class areDefineandCompilerFlag

Note

If you generate code withstartupactivation time, specify the supported custom storage class for the objects. For more information on built-in and custom storage classes supported withstartupactivation time seeStorage Classes for Different Variant Activation Times.

.

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');