Main Content

Code Generation for Enumerations

Enumerations represent a fixed set of named values.Enumerations help make your MATLAB®code and generated C/C++ code more readable. For example, the generated code can test equality with code such asif (x == Red)instead of usingstrcmp.To generate C/C++ code, you must have万博1manbetx®Coder™.

When you use enumerations in aMATLAB Functionblock, adhere to these restrictions:

  • Calls to methods of enumeration classes are not supported.

  • Passing strings or character vectors to constructors of enumerations is not supported.

  • For aMATLAB Functionblock, you can import an externally defined type by usingSimulink.defineIntEnumTypeor you can define an enumeration class. The enumeration class must derive from one of these base types:Simulink.IntEnumType,int8,uint8,int16,uint16, orint32. SeeDefine Enumerations for MATLAB Function Blocks.

  • You can use only a limited set of operations on enumerations. SeeAllowed Operations on Enumerations.

  • Use enumerations with functions that support enumerated types for code generation. SeeMATLAB Toolbox Functions That Support Enumerations.

Define Enumerations forMATLAB FunctionBlocks

You can define enumerations forMATLAB Functionblocks in two ways:

  • To import an externally defined enumeration, use theSimulink.defineIntEnumTypefunction. SeeImport Enumerations Defined Externally to MATLAB.

  • In a class definition file, define an enumerated type. For example:

    classdef PrimaryColors <Simulink.IntEnumType枚举红(1),蓝色(2),黄色(4)结束

If you define an enumerated type in a class definition file, the class must derive from one of these base types:Simulink.IntEnumType,int8,uint8,int16,uint16, orint32. Then, you can exchange enumerated data betweenMATLAB Functionblocks and other Simulink blocks in a model.

If you useSimulink Coderto generate C/C++ code, you can use the enumeration class base type to control the size of an enumerated type in generated C/C++ code. You can:

  • Represent an enumerated type as a fixed-size integer that is portable to different targets.

  • Reduce memory usage.

  • Interface with legacy code.

  • Match company standards.

The base type determines the representation of the enumerated type in generated C/C++ code.

If the base type isSimulink.IntEnumType, the code generator produces a C enumeration type. Consider the following MATLAB enumerated type definition:

classdefLEDcolor < Simulink.IntEnumTypeenumerationGREEN(1), RED(2)endend

This enumerated type definition results in the following C code:

typedef enum { GREEN = 1, RED } LEDcolor;
For built-in integer base types, the code generator produces atypedefstatement for the enumerated type and#define枚举值的语句。考虑到following MATLAB enumerated type definition:
classdefLEDcolor < int16enumerationGREEN(1), RED(2)endend
This enumerated type definition results in the following C code:
typedef int16_T LEDcolor; #define GREEN ((LEDcolor)1) #define RED ((LEDcolor)2)

To customize the code generated for an enumerated type, seeCustomize Simulink Enumeration.

Allowed Operations on Enumerations

For code generation, you are restricted to the operations on enumerations listed in this table.

Operation Example Notes

assignment operator:=

xon = LEDcolor.GREEN xoff = LEDcolor.RED

relational operators:< > <= >= == ~=

xon == xoff

Code generation does not support using==or~=to test equality between an enumeration member and a string array, a character array, or a cell array of character arrays.

cast operation

double(LEDcolor.RED)

conversion to character array or string

y = char(LEDcolor.RED); y1 = cast(LEDcolor.RED,'char'); y2 = string(LEDcolor.RED);

  • You can convert only compile-time scalar valued enumerations. For example, this code runs in MATLAB, but produces an error in code generation:

    y2 = string(repmat(LEDcolor.RED,1,2));
  • The code generator preserves enumeration names when the conversion inputs are constants. For example, consider this enumerated type definition:

    classdef AnEnum < int32 enumeration zero(0), two(2), otherTwo(2) end end

    Generated code produces"two"for

    y = string(AnEnum.two)
    and"otherTwo"for
    y = string(AnEnum.otherTwo)

indexing operation

m = [1 2] n = LEDcolor(m) p = n(LEDcolor.GREEN)

control flow statements: if, switch, while

if state == sysMode.ON led = LEDcolor.GREEN; else led = LEDcolor.RED; end

MATLABToolbox Functions That Support Enumerations

For code generation, you can use enumerations with these MATLAB toolbox functions:

Related Topics