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 as if (x == Red)
instead
of using strcmp
.
For code generation, when you use enumerations, 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.
The enumeration class must derive from one of these base types:
int8
, uint8
, int16
,
uint16
, or int32
. See Define Enumerations for Code Generation.
You can use only a limited set of operations on enumerations. See Allowed Operations on Enumerations.
Use enumerations with functions that support enumerated types for code generation. See MATLAB Toolbox Functions That Support Enumerations.
For code generation, the enumeration class must derive from
one of these base types: int8
, uint8
, int16
, uint16
,
or int32
. For example:
classdef PrimaryColors < int32 enumeration Red(1), Blue(2), Yellow(4) end end
You can use the 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 is the native integer type for the target platform (for example,
int32
), the code generator produces a C/C++ enumerated type.
Consider this MATLAB enumerated type definition:
classdef LEDcolor < int32 enumeration GREEN(1), RED(2) end end
This enumerated type definition results in the following C/C++ code:
enum LEDcolor { GREEN = 1, RED }; typedef enum LEDcolor LEDcolor;
For built-in integer base types that are different from the native integer type for the target platform:
If you generate C code, the code generator produces a
typedef
statement for the enumerated type and
#define
statements for the enumerated values.
Consider this MATLAB enumerated type
definition:
classdef LEDcolor < int16 enumeration GREEN(1), RED(2) end end
typedef short LEDcolor; #define GREEN ((LEDcolor)1) #define RED ((LEDcolor)2)
If you generate C++ code, the enumeration members are converted to constants. These constants belong to the namespace that contains the enumeration type definition in the generated C++ code.
For example, suppose that you place this MATLAB enumerated type definition inside the package
pkg
:
classdef LEDcolor < int16 enumeration GREEN(1), RED(2) end end
pkg
:namespace pkg { typedef short LEDcolor; // enum pkg_LEDcolor const LEDcolor GREEN{1}; const LEDcolor RED{2}; }
The C/C++ type in the typedef
statement depends on:
The integer sizes defined for the production hardware in the hardware
implementation object or the project settings. See coder.HardwareImplementation
.
The setting that determines the use of built-in C types or MathWorks® typedefs in the generated code. See Specify Data Types Used in Generated Code and Mapping MATLAB Types to Types in Generated Code.
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 |
cast operation |
double(LEDcolor.RED) |
— |
conversion to character array or string |
y = char(LEDcolor.RED); y1 = cast(LEDcolor.RED,'char'); y2 = string(LEDcolor.RED); |
|
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 |
— |
For code generation, you can use enumerations with these MATLAB toolbox functions: