Customize Enumerated Types in Generated Code
For code generation, to customize an enumeration, in the static methods section of the class definition, include customized versions of the methods listed in this table.
Method | Description | Default Value Returned or Specified | When to Use |
---|---|---|---|
| Returns the default enumerated value. | First value in the enumeration class definition. | For a default value that is different than the first
enumeration value, provide a |
| Specifies the file that defines an externally defined enumerated type. |
| To use an externally defined enumerated type, provide
a |
| Specifies whether the class name becomes a prefix in the generated code. |
| If you want the class name to become a prefix in the generated code, set the return
value of the Note When generating enumeration classes (C++11 and newer standards), the code generator ignores this static method. |
generateEnumClass | Specifies whether to generate enumeration classes | true — enumeration classes are generated in
C++ code | When generating C++11 (or newer) code, to instruct the code generator
to produce an ordinary C enumeration for a particular MATLAB® enumeration, set the return value of the
generateEnumClass method to
false . See Generate C++ Code That Contains Ordinary C Enumeration (MATLAB Coder). |
Specify a Default Enumeration Value
If the value of a variable that is cast to an enumerated type does not match one of the enumerated type values:
Generated MEX reports an error.
Generated C/C++ code replaces the value of the variable with the enumerated type default value.
Unless you specify otherwise, the default value for an enumerated
type is the first value in the enumeration class definition. To specify
a different default value, add your own getDefaultValue
method
to the methods section. In this example, the first enumeration member
value is LEDcolor.GREEN
, but the getDefaultValue
method
returns LEDcolor.RED
:
classdef LEDcolor < int32 enumeration GREEN(1), RED(2) end methods (Static) function y = getDefaultValue() y = LEDcolor.RED; end end end
Specify a Header File
To specify that an enumerated type is defined in an external
file, provide a customized getHeaderFile
method.
This example specifies that LEDcolor
is defined
in the external file my_LEDcolor.h
.
classdef LEDcolor < int32 enumeration GREEN(1), RED(2) end methods(Static) function y=getHeaderFile() y='my_LEDcolor.h'; end end end
You must provide my_LEDcolor.h
. For example:
enum LEDcolor { GREEN = 1, RED }; typedef enum LEDcolor LEDcolor;
If you place the MATLAB enumeration LEDcolor
inside the MATLAB namespace nmsp
and generate C++ code, code
generation preserves the name of this enumeration and places it inside the C++
namespace nmsp
in the generated code. Therefore, in the
header file that you provide, you must define this enumeration inside the
namespace nmsp
.
Include Class Name Prefix in Generated Enumerated Type Value Names
By default, the generated enumerated type value name does not include the class name prefix. For example:
enum LEDcolor { GREEN = 1, RED }; typedef enum LEDcolor LEDcolor;
To include the class name prefix, provide an addClassNameToEnumNames
method
that returns true
. For example:
classdef LEDcolor < int32 enumeration GREEN(1), RED(2) end methods(Static) function y = addClassNameToEnumNames() y=true; end end end
In the generated type definition, the enumerated value names
include the class prefix LEDcolor
.
enum LEDcolor { LEDcolor_GREEN = 1, LEDcolor_RED }; typedef enum LEDcolor LEDcolor;
Generate C++ Code That Contains Ordinary C Enumeration
When you generate code using a language standard that supports enumeration classes (C++11 or newer), your MATLAB enumeration class is converted to an enumeration class. For example:
enum class MyEnumClass16 : short { Orange = 0, // Default value Yellow, Pink };
To generate an ordinary C enumeration instead, provide a
generateEnumClass
method that returns
false
. For example:
classdef MyEnumClass16 < int16 enumeration Orange(0), Yellow(1), Pink(2) end % particular enum opting out methods(Static) function y = generateEnumClass() y = false; end end end
Now the generated code contains an ordinary C enumeration.
enum MyEnumClass16 : short { Orange = 0, // Default value Yellow, Pink };