Main Content

Create Variant Controls Using an In-Memory Enumeration Class

Each variant choice in a Simulink® model is associated with a conditional expression called a variant control. The variant control that evaluates to true determines the active variant choice in the model. This example shows how to create variant controls in your model using an in-memory enumeration class. The enumeration is created using the Simulink.VariantConfigurationData function. You do not need to save the enumeration in a MATLAB® file. In code generation, enumerated types improve readability because condition values are represented as meaningful names instead of literal values.

Explore the Model

Open the model slexVariantSetupUsingInMemoryEnums. It contains two variant choices, Linear controller and Nonlinear controller. Variant Sink and Variant Source blocks are used to implement the variant regions.


Specify the Enumeration Class in the PreLoad Callback of the Model

In this example, the enumeration class Controller is defined in the model PreLoad callback function. The enumeration class defines two enumeration members, Linear and Nonlinear, with underlying integer values 0 and 1. The enumeration definition is exported to a header file named Controller.h during code generation. The MATLAB variable V is used as the variant control variable to store the currently active variant choice.

    {'Linear', 'Nonlinear'}, [0;1], 'Description', 'Controller',...
    'DefaultValue', 'Linear', 'HeaderFile', 'Controller.h', 'DataScope',...
V = Controller.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 select Open in Variant Manager .

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

See Also