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
. To generate C/C++ code, you must have Simulink®
Coder™.
When you use
enumerations in a MATLAB Function
block, 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 a MATLAB Function block, you can import an externally defined
type by using Simulink.defineIntEnumType
or you can define an
enumeration class. The enumeration class must derive from one of these base types:
Simulink.IntEnumType
, int8
,
uint8
, int16
, uint16
,
or int32
. See Define Enumerations for MATLAB Function Blocks.
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.
You can define enumerations for MATLAB Function blocks in two ways:
To import an externally defined enumeration, use the Simulink.defineIntEnumType
function.
See Import Enumerations Defined Externally to MATLAB.
In a class definition file, define an enumerated type. For example:
classdef PrimaryColors < Simulink.IntEnumType
enumeration
Red(1),
Blue(2),
Yellow(4)
end
end
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
,
or int32
. Then, you can exchange enumerated data
between MATLAB Function blocks and other Simulink blocks
in a model.
If you use Simulink Coder to 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 is Simulink.IntEnumType
, the
code generator produces a C enumeration type. Consider the following MATLAB enumerated
type definition:
classdef LEDcolor < Simulink.IntEnumType enumeration GREEN(1), RED(2) end end
This enumerated type definition results in the following C code:
typedef enum { GREEN = 1, RED } LEDcolor;
typedef
statement for the
enumerated type and #define
statements for the
enumerated values. Consider the following MATLAB enumerated type
definition:classdef LEDcolor < int16 enumeration GREEN(1), RED(2) end end
typedef int16_T LEDcolor; #define GREEN ((LEDcolor)1) #define RED ((LEDcolor)2)
To customize the code generated for an enumerated type, see Customize Simulink Enumeration.
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: