Skip to Main Content Skip to Search
Product Documentation

Setting Up Variant Control

Creating Control Variables

You must create variant control variables to control which variant is active. You create variant control variables in the same way for variant subsystems or variant models.

To specify the condition of a variant object, you can use MATLAB variables, or Simulink.Parameter objects that reside in the base workspace. The conditions of the variant objects determine which variant is the active variant. At the MATLAB command line or in the Model Explorer, create variant control variables to match your specified conditions. Before compiling or simulating, you set the variant control variables to values that specify the environment in which you want to simulate.

At the MATLAB Command Window or in the Model Explorer, create variant control variables:

Each technique creates the variant control variables in the base workspace.

Saving Variant Components

Variant control variables and variant objects exist in the base workspace. If you want to reload variant control variables or variant objects with your model, you must save them to a MAT-file. For more information, see Exporting to MAT-Files.

Example Variant Control Variables

The example AutoMRVar uses the control variables EMIS and FUEL. Depending on the values of EMIS and FUEL, you can see which variant is active.

Variant Control VariablesVariant ObjectActive Variant (Model Block or Subsystem Block)
FUEL=1 and EMIS=1GUGasolUSA
FUEL=1 and EMIS=2GEGasolEuro
FUEL=2 and EMIS=1DUDieselUSA
FUEL=2 and EMIS=2DEDieselEuro

To try an example changing control variables:

  1. Open AutoMRVar.mdl.

  2. Specify FUEL=2 and EMIS=2 in the base workspace. When you choose these values for the variant control variables, the variant condition associated with the variant object DE is true. Therefore, the active variant is DieselEuro.mdl, and the model AutoMRVar uses DieselEuro as its referenced model during simulation.

  3. Select Edit > Update Diagram to see the active variant update.

For an example explaining control variables for variant subsystems, including the use of enumerated types, see the demo sldemo_variant_subsystems.mdl.

Using Enumerated Types for Variant Control Variable Values

You can use enumerated types to give meaningful names to the integers that you use as values of variant control variables. For more information about defining and using enumerated types, see Enumerations and Modeling. For example, suppose you defined the following enumerated class, whose elements represent vehicle properties:

classdef(Enumeration) VarParams < Simulink.IntEnumType
  enumeration
    gasoline(1)
    diesel(2)
    USA(1)
    European(2)
  end
end

With the class VarParams defined, you can use meaningful names to specify variant control variables and variant conditions. For example, you can substitute names for the integers for variant control variables EMIS and FUEL.

EMIS = VarParams.USA
FUEL = VarParams.diesel

Using the techniques described in About Variant Objects, the variant objects are:

GU=Simulink.Variant('FUEL==VarParams.gasoline && EMIS==VarParams.USA')
GE=Simulink.Variant('FUEL==VarParams.gasoline && EMIS==VarParams.European')
DU=Simulink.Variant('FUEL==VarParams.diesel && EMIS==VarParams.USA')
DE=Simulink.Variant('FUEL==VarParams.diesel && EMIS==VarParams.European')

Specifying meaningful names rather than integers as the values of variant control variables facilitates creating complex variant specifications. It also clarifies the generated code, which contains the names of the values rather than integers.

For an example explaining the use of enumerated types with variants, see the demo sldemo_variant_subsystems.mdl.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS