Main Content

Create Variant Controls Programmatically

Create and Export Variant Controls

Create control variables, define variant conditions, and export control variables.

  1. Create control variables in the base workspace or a data dictionary.

  2. Use the control variables to define the control condition using a Simulink.VariantExpression object.

    LinearController=Simulink.Variant('FUEL==2 && EMIS==1');


    Before each simulation, define Simulink.VariantExpression objects representing the variant conditions.

  3. If you saved the variables in the base workspace, select the control variables to export in the workspace. Right-click and click Save As to specify the name of a MAT-file.

Reuse Variant Conditions

If you want to reuse common variant conditions across models, specify variant control conditions using Simulink.VariantExpression objects.

Reuse Simulink.VariantExpression objects to change the model hierarchy dynamically to reflect variant conditions by changing the values of the control variables that define the condition expression.

The example Simulink.VariantExpression Objects for Variant Condition Reuse of Variant Blocks shows the use of Simulink.Variant objects to define variant control conditions.


You must use Simulink.VariantExpression objects to define variant control conditions for AUTOSAR workflows.

Enumerated Types as Variant Controls

Use enumerated types to give meaningful names to integers used as variant control values.

  1. In the MATLAB® Editor, define the classes that map enumerated values to meaningful names.

    classdef sldemo_mrv_CONTROLLER_TYPE < Simulink.IntEnumType
            NONLINEAR (1)
            SECOND_ORDER (2)
    classdef sldemo_mrv_BUILD_TYPE < Simulink.IntEnumType
            PROTOTYPE (1)
            PRODUCTION (2)
  2. Define Simulink.VariantExpression objects for these classes in the base workspace.

    VE_NONLINEAR_CONTROLLER = Simulink.VariantExpression...
    VE_SECOND_ORDER_CONTROLLER =Simulink.VariantExpression...
    VE_PROTOTYPE =Simulink.VariantExpression...
    VE_PRODUCTION =Simulink.VariantExpression...

    Using enumerated types simplifies the generated code because it contains the names of the values rather than integers.

Related Examples

More About