Main Content

Create a Simple Variant Model

  1. Create a model that contains variant blocks. For example, seeVariant Subsystemsthat contains a Variant Subsystem block (Controller).

  2. Define variant control variables that determine the condition under which a variant choice is active.

    1. Right-click the variant block and clickBlock Parameters. The Block Parameters dialog box for the variant block opens.

    2. To choose the active Variant based on the evaluation of the Variant conditions, use theExpressionmode else selectLabelmode. When you select theVariant control modeasLabel,Label mode active choiceoption is available. InLabelmode, Variant control need not be created in the global workspace. You can select an active Variant choice fromLabel mode active choiceoptions.

    3. Use the options available on the Block Parameter dialog box to add variant controls and its corresponding variant condition.

      A sample screenshot forExpressionmode:

      A sample screenshot forLabelmode:

      Note

      The variables used to specify the variant control and variant condition must be defined in the global workspace, model workspace, mask workspace or data dictionary for the model.

  3. Specify a default variant condition to be used when there is no active variant choice. Use theVariant controllist to specify the default.

  4. To activate a variant choice, type the variant choice in MATLAB®command window. For example, typeVSS_MODE = 2.

  5. To simulate the model, on the toolstrip, clickRun. The model simulates for the specified active choice.

  6. Modify the active choice and simulate the model again, if necessary.

  7. Generate code for the variants model with preprocessor conditionals.

    Note

    You must have an Embedded Coder®license to generate code.

    1. In the Block Parameters dialog box, from theVariant activation timelist, selectcode compile.

    2. Open the Subsystem Block Parameters dialog boxes. Select theTreat as atomic unitparameter.

    3. In theCode Generationsection of Configuration Parameters dialog box, specify theSystem target fileasert.tlc.

    4. In Model Explorer, define the variables used to specify the variant choice as a MATLAB variable or as aSimulink.Parameter. The data type of theSimulink.Parametercan be of typeInteger,Boolean, orEnumeratedand the storage class can be eitherimportedDefine(Custom),Define(Custom), orCompilerFlag.

  8. For the variants that are defined in the global workspace, export the control variables to a MAT-file. For example, type the following in the MATLAB command window:

    1. save  

    2. PostLoadCallback > load 

Note

To update or refresh active models that containVariant Subsystemblocks, on theModelingtab, clickUpdate Model(Ctrl + D) in Simulink®.

Related Examples

More About