Main Content

Represent Variant Condition Values of Enumerated Type in Generated Code

You can generate C code from a Simulink® model with variant condition variables or values of Simulink-supported Use Enumerated Data in Simulink Models. Enumerated types improve code readability because the condition values and variables in control expressions are represented as meaningful names instead of integers. In this section, you will learn:

  • How to generate C code from a model with variant condition values of enumerated types.

  • How the variant condition values are represented in the generated code.

Use Enumerated Type Derived From Built-In Integer

This example shows how to generate C code from a Simulink model with variant condition values of enumerated type derived from the built-in integer int32.

Open the slexVariantSetupUsingIntEnums model. This model runs the PostLoadFcn defined in the ModelProperties > Callbacks dialog box. This function populates the base workspace with the control variables for the variant blocks.

modelName = 'slexVariantSetupUsingIntEnums';
open_system(modelName)

In this model, the Variant Sink and the Variant Source blocks specify two potential variants for the Linear controller and Nonlinear controller subsystems. The control expression for the Linear controller subsystem is V == ControllerChoice.Linear. The control expression for the Nonlinear controller subsystem is V == ControllerChoice.NonLinear. Here, ControllerChoice is an integer-based enumeration class derived from the built-in data type, int32. The class is defined in the ControllerChoice.m file. The class has two enumeration values, Linear and Nonlinear. These enumerated values have underlying integer values 0 and 1.

Switch Between Variant Choices and Generate Code

1. Click Simulation > Run and see the variant conditions being propagated to the blocks. By default, the model simulates for the Linear controller subsystem.

sim(modelName);

2. To modify the active choice, set the value of V to ControllerChoice.Nonlinear, then simulate the model again. The model simulates for the Nonlinear controller subsystem.

V = ControllerChoice.Nonlinear;
sim(modelName);

3. Before you generate code from the model, you must have write permission in your current folder.

a. To generate code, in the Apps gallery of the model toolstrip, click Embedded Coder. The C Code tab appears.

b. Click the Generate Code > Build icon.

c. In the C Code tab of the toolstrip, select Open Report.

In the generated code, the variant control values ControllerChoice.Linear and ControllerChoice.Nonlinear are converted to ControllerChoice_Linear and ControllerChoice_Nonlinear, respectively. A valid C processor recognizes this format. The code also includes the definitions of macros corresponding to these variants. The active variant is determined by using preprocessor conditionals (#if) on the macros (#define) ControllerChoice_Linear and ControllerChoice_Nonlinear.

  #if V == ControllerChoice_Linear
  slexVariantSetupUsingIntEnums_B.VariantMerge_For_Variant_Source =
  slexVariantSetupUsingIntEnums_P.DiscreteTransferFcn_NumCoef_a *
  slexVariantSetupUsingIntEnum_DW.DiscreteTransferFcn_states_o;
  #endif
  #if V == ControllerChoice_Nonlinear
  slexVariantSetupUsingIntEnums_B.VariantMerge_For_Variant_Source =
  look1_binlxpw(slexVariantSetupUsingIntEnums_P.DiscreteTransferFcn_NumCoef_l *
  slexVariantSetupUsingIntEnum_DW.DiscreteTransferFcn_states_n,
  slexVariantSetupUsingIntEnums_P.uDLookupTable_bp01Data,
  slexVariantSetupUsingIntEnums_P.uDLookupTable_tableData, 10U);
  #endif

To view the definitions of macros, ControllerChoice_Linear and ControllerChoice_Nonlinear, click the Controller.h file in the Generated Code pane of the report.

  #define ControllerChoice_Linear     (0)  /* Default value */
  #define ControllerChoice_Nonlinear  (1)
  #endif                                  /* RTW_HEADER_Controller_h_ */

To view the definition of the variant control variable, V, click the slexVariantSetupUsingIntEnums_types.h file.

  #ifndef V
  #define V     ControllerChoice_Linear
  #endif

Use In-Memory Enumerated Type Derived From Simulink.IntEnumType

The Simulink model in this example shows how to generate C code from a model having variant control variables of enumerated type derived from Simulink.IntEnumType.

1. Open the slexVariantSetupUsingInMemoryEnums model. The model runs the PreLoadFcn defined in the ModelProperties > Callbacks dialog box. This populates the base workspace with the control variables for the variant blocks.

open_system('slexVariantSetupUsingInMemoryEnums')

2. Generate C code. The variant control variables Controller.Linear and Controller.Nonlinear are represented as Controller_Linear and Controller_Nonlinear in the generated code, as the above example also shows. The macros Controller_Linear and Controller_Nonlinear are defined in the Controller.h file.

  #define Controller_Linear     (0)  /* Default value */
  #define Controller_Nonlinear  (1)
  #endif                                  /* RTW_HEADER_Controller_h_ */

The variant control variable is defined in the slexVariantSetupUsingInMemoryEnums_types.h file.

  #ifndef V
  #define V         Controller_Linear
  #endif

See Also

Represent Variant Source and Sink Blocks in Generated Code (Embedded Coder)