Main Content

Create Variant Controls Using MATLAB® Enumeration Class

Each variant choice in a Simulink® model is associated with a conditional expression called avariant control。评估为true潜艇雷达的版本控制rmines the active variant choice in the model. This example shows how to create variant controls in your model using a MATLAB® enumeration in the variant condition expressions. In code generation, enumerated types improve readability because condition values are represented as meaningful names instead of literal values.

Explore the Model

Open the modelslexVariantSetupUsingIntEnums。It contains two variant choices,线性控制erandNonlinear controller。Variant Sink and Variant Source blocks are used to implement the variant regions.

Specify the Enumeration Class in a MATLAB® File

You can create an enumeration class by adding an enumeration block to a class definition and saving it in a MATLAB file. In this example, the enumerationControllerChoice(saved inControllerChoice.m) derives from the built-in numeric typeint32。It defines two enumeration members,LinearandNonlinear

classdefControllerChoice < int32enumerationLinear (0), Nonlinear (1),end%All the methods below can be optionally used to configure%the enumeration in the generated codemethods(静态= true)%% Description of the enumerationfunctionretVal = getDescription() retVal ='Controller...';end%% Default value of the enumerationfunctionretVal = getDefaultValue() retVal = ControllerChoice.Linear;end%% Specify whether the generated code imports/exports the definition%% of the enum used in the variant control expressionfunctionretVal = getDataScope() retVal ='Exported';end%% Get the header file to import from/export to the definition of the%% enumeration in the generated codefunctionretVal = getHeaderFile() retVal ='Controller.h';endendend

Create a Variant Control Variable

This example uses the MATLAB variableVas the variant control variable to store the variant choice that is currently active.

In the MATLAB Command Window, type:

V = ControllerChoice.Linear;

Build the Variant Condition Expression

You can set the variant controls from the Variant Manager. To open the Variant Manager, right-click the variant badge on the Variant Sink or Variant Source block and selectOpen in Variant Manager

You can see how the enumeration members and the control variable is used to create the condition expressionsV == ControllerChoice.LinearandV == ControllerChoice.Nonlinear。All supported Simulink enumerations can be used to build the condition expression. For information on enumerations in Simulink, seeSimulink Enumerations

See Also