Code Generation for Enumerations - MATLAB & Simulink (original) (raw)

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:

Define Enumerations for Code Generation

For code generation, the enumeration class must derive from one of these base types:int8, uint8, int16,uint16, int32, or uint32. 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:

Representation of Enumerated Type in Generated Code

C++11 and newer standards support enumeration classes. If you generate C++ code and set the target language standard to C++11 or newer, the code generator converts MATLAB enumerations to enumeration classes in the generated C++ code. Otherwise, the code generator converts MATLAB enumerations to ordinary C enumerations in the generated C/C++ code. Therefore, the representation of the enumerated type in the generated C/C++ code depends on:

To change the target language standard, see Change Language Standard Used for Code Generation.

Base Type is Native Integer Type. 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

If you generate C code or C++03 code, the generated enumeration is:

enum LEDcolor
{
    GREEN = 1,
    RED
};

If you generate C++11 or newer code, the generated code contains an enumeration class (by default) that explicitly defines the underlying type:

enum class LEDcolor : int
{
  GREEN = 1, 
  RED
};

Base Type is Different from the Native Integer Type. Suppose that built-in integer base type for the enumeration is different from the native integer type for the target platform. For example, 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)  
namespace nmsp {  
typedef short LEDcolor;  
// enum nmsp_LEDcolor  
const LEDcolor GREEN{1};  
const LEDcolor RED{2};  
}  
namespace nmsp {  
enum class LEDcolor : short  
{  
  GREEN = 1, // Default value  
  RED  
};  
}  

The C/C++ type in the typedef statement (for C and C++03 code) or the underlying type of the enumeration class (for C++11 and newer code) depends on:

Generate C++ Code That Contains Ordinary C Enumerations. You can instruct the code generator to produce ordinary C enumerations, even if you generate code with a language standard that supports enumeration classes, such as C++11 or newer. To change the default, use one of these approaches:

To instruct the code generator to produce ordinary C enumeration for a particular MATLAB enumeration class in your code, include the static methodgenerateEnumClass that returns false in the implementation of that MATLAB enumeration class. See Customize Enumerated Types in Generated Code.

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 endGenerated code produces "two" fory = string(AnEnum.two) and "otherTwo" fory = 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

MATLAB Toolbox Functions That Support Enumerations

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