Main Content

Enumeration

To generate an enumerated data type, define an enumeration class in a MATLAB® file. Then, use the enumeration class as the data type of signals, block parameters, and states in your model.

C Construct

typedef enum {
  Choice1 = 0,
  Choice2
} myEnumType;

Procedure

In your current folder, create the MATLAB file ex_myEnumType.m. The file defines an enumeration class ex_myEnumType.

classdef ex_myEnumType < Simulink.IntEnumType
    
    enumeration
        Choice1(0)
        Choice2(1)
    end %enumeration
    
    methods (Static)
        function retVal = getHeaderFile()
            retVal = 'myEnumHdr.h';
        end %function
        
        function retVal = getDataScope()
            retVal = 'Exported';
        end %function
    end %methods
    
end %classdef

1. Open the model ex_pattern_enum that has an Enumerated Constant block and a Multiport Switch block.

2. In the Model Data Editor, select the Parameters tab.

3. In the model, click the Enumerated Constant block.

4. In the Model Data Editor, use the Value column to set the constant value to myChoice. Next to myChoice, click the action button (with three vertical dots) and select Create.

5. In the Create New Data dialog box, set Value to Simulink.Parameter(ex_myEnumType.Choice1) and click Create. A Simulink.Parameter object named myChoice appears in the base workspace. The object stores the enumerated value Choice1 of the type ex_myEnumType.

6. On the Code Generation tab, set Storage Class to ExportedGlobal. With this setting, the object appears in the generated code as a global variable.

7. In the Model Data Editor, select the Signals tab.

8. In the model, select the output signal of the Enumerated Constant block.

9. In the Model Data Editor, use the Data Type column to set the signal data type to Enum: ex_myEnumType.

10. Select the Multiport Switch block.

11. In the Property Inspector, set:

  • Data port order to Specify indices.

  • Data port indices to enumeration('ex_myEnumType'). This expression returns all of the enumeration members of ex_myEnumType.

12. Set the model configuration parameter File packaging format to Modular. With this setting, in the generated code, the definition of ex_myEnumType can appear in the specified header file, myEnumHdr.h.

13. To build the model and generate code, press Ctrl+B.

Results

View the generated header file myEnumHdr.h. The file defines the enumerated data type.

typedef enum {
  Choice1 = 0,                         /* Default value */
  Choice2
} ex_myEnumType;

View the source file ex_pattern_enum.c. The file defines the variable myChoice. The algorithm in the step function uses myChoice to route one of the input signals to the output signal.

ex_myEnumType myChoice = Choice1;      /* Variable: myChoice

/* Model step function */
void ex_pattern_enum_step(void)
{
  /* MultiPortSwitch: '<Root>/Multiport Switch' incorporates:
   *  Constant: '<S1>/Constant'
   */
  if (myChoice == Choice1) {
    /* Outport: '<Root>/Data Out' incorporates:
     *  Inport: '<Root>/Data In 1'
     */
    rtY.DataOut = rtU.DataIn1;
  } else {
    /* Outport: '<Root>/Data Out' incorporates:
     *  Inport: '<Root>/Data In 2'
     */
    rtY.DataOut = rtU.DataIn2;
  }

  /* End of MultiPortSwitch: '<Root>/Multiport Switch' */
}

See Also

Related Topics