| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Embedded MATLAB |
| Contents | Index |
Enumerated data is data that has a finite set of values. An enumerated data type is a user-defined type whose values belong to a predefined set of symbols, also called enumerated values.
The Embedded MATLAB subset supports integer-based enumerated types, where each enumerated value consists of a name and an underlying numeric value. You define an enumerated type as a MATLAB class; the instances of the class represent the values that comprise the enumerated type.
For example, the following MATLAB script defines an integer-based enumerated data type named PrimaryColors:
classdef(Enumeration) PrimaryColors < int32
enumeration
Red(1),
Blue(2),
Yellow(4)
end
endIn this example, the statement classdef(Enumeration) PrimaryColors < int32 means that the enumerated type PrimaryColors is based on the built-in type int32. As such, PrimaryColors inherits the characteristics of the int32 type, as well as defining its own unique characteristics. For example, PrimaryColors is restricted to three enumerated values:
| Enumerated Value | Enumerated Name | Underlying Numeric Value |
|---|---|---|
| Red(1) | Red | 1 |
| Blue(2) | Blue | 2 |
| Yellow(4) | Yellow | 4 |
The Embedded MATLAB subset supports another enumerated type, based on the built-in type Simulink.IntEnumType, which requires a Simulink license.
Defining and Organizing Classes in the MATLAB Object-Oriented Programming documentation for more information about defining MATLAB classes
Enumerated Data in Simulink for more information about enumerated types based on Simulink.IntEnumType
You can use enumerated types to represent program states and to control program logic, especially when you need to restrict data to a finite set of values and refer to these values by name. Even though you can sometimes achieve these goals by using integers or strings, enumerated types offer the following advantages:
Provide more readable code than integers
Allow more robust error checking than integers or strings
For example, if you mistype the name of an element in the enumerated type, the Embedded MATLAB subset alerts you that the element does not belong to the set of allowable values.
Produce more efficient code than strings
For example, comparisons of enumerated values execute faster than comparisons of strings.
Enumerated types in MATLAB are user-defined types that are based on built-in data types. The enumerated type inherits the characteristics of the built-in type, but also defines its own unique characteristics. For an example, see Enumerated Data in the Embedded MATLAB Subset.
The Embedded MATLAB subset supports two integer-based enumerated data types, each of which inherits characteristics from a different built-in type:
This enumerated data type is based on the built-in type int32. Use this enumerated type when generating C-MEX functions from M-code that is compliant with the Embedded MATLAB subset. You can use C-MEX functions for rapid prototyping and verification of generated C code within MATLAB.
classdef(Enumeration) type_name < int32classdef(Enumeration) PrimaryColors < int32
enumeration
Red(1),
Blue(2),
Yellow(4)
end
endHow to Use. Define enumerated data in M-code and compile your M-file with emlmex, as described in Using Enumerated Data in Embedded MATLAB Compliant M-Code.
This enumerated data type is based on the built-in type Simulink.IntEnumType, which is available with a Simulink license. Use this enumerated type when:
Exchanging enumerated data with Simulink blocks and Stateflow charts
Generating embeddable C code from Embedded MATLAB compliant M-code
classdef(Enumeration) type_name < Simulink.IntEnumTypeclassdef(Enumeration) myMode < Simulink.IntEnumType
enumeration
OFF(0)
ON(1)
end
endHow to Use. Here are the basic guidelines for using enumerated data based on Simulink.IntEnumType:
| Application | What to Do |
|---|---|
When exchanging enumerated data with Simulink blocks | Define enumerated data in Embedded MATLAB Function blocks in Simulink models. Requires Simulink software. |
When exchanging enumerated data with Stateflow charts | Define enumerated data in Embedded MATLAB functions in Stateflow charts. Requires Simulink and Stateflow software. |
When generating embeddable C code from Embedded MATLAB compliant M-code | Define enumerated data in M-code and compile your M-file with emlc. Requires Real-Time Workshop software. |
For more information, see:
Using Enumerated Data in Embedded MATLAB Function Blocks in the Simulink documentation
Using Enumerated Data in Stateflow Charts in the Stateflow documentation
How emlc Works with Enumerated Types in the Real-Time Workshop documentation
The following example appears throughout this section to illustrate how to define and use enumerated types. It is a simple M-function, displayState, that uses enumerated data to represent the modes of a device that controls the colors of an LED display.
Before you can use enumerated data in Embedded MATLAB compliant M-code, you must define your enumerated data types as MATLAB classes that inherit from the built-in type int32. Each class definition must reside in a separate M-file on the MATLAB path. This example uses two enumerated types: sysMode to represent the set of allowable modes and LEDcolor to represent the set of allowable colors.
Here is the class definition of the sysMode enumerated data type:
classdef(Enumeration) sysMode < int32
enumeration
OFF(0)
ON(1)
end
endThis definition must reside on the MATLAB path in an M-file with the same name as the class, sysMode.m.
Here is the class definition of the LEDcolor enumerated data type:
classdef(Enumeration) LEDcolor < int32
enumeration
GREEN(1),
RED(2),
end
endThis definition must reside on the MATLAB path in an M-file called LEDcolor.m.
The following M-function displayState uses enumerated data to activate an LED display, based on the state of a device. It lights a green LED display to indicate the ON state and lights a red LED display to indicate the OFF state.
function led = displayState(state)
%#eml
if state == sysMode.ON
led = LEDcolor.GREEN;
else
led = LEDcolor.RED;
endThis function models a simple control.
Compiler directive %#eml ensures compliance with the Embedded MATLAB subset. See Adding the Compilation Directive %#eml.
Here is the basic workflow for using enumerated data when generating C-MEX functions from Embedded MATLAB compliant M-code:
| Step | Action | How? |
|---|---|---|
Define an enumerated data type that inherits from int32. | ||
Instantiate the enumerated type in your Embedded MATLAB compliant M-code. | See How to Instantiate Enumerated Types in the Embedded MATLAB Subset. | |
Compile the function with emlmex. |
Follow these steps:
Create a class definition file.
In the MATLAB Command Window, select File > New > Class M-File.
Enter the class definition as follows:
classdef(Enumeration) EnumTypeName < int32
For example, the following code defines an enumerated type called sysMode:
classdef(Enumeration) sysMode < int32
...
endEnumTypeName is a case-sensitive string that must be unique among data type names and workspace variable names. It must inherit from the built-in type int32.
Define enumerated values in an enumeration section as follows:
classdef(Enumeration) EnumTypeName < int32
enumeration
EnumName(N)
...
end
endFor example, the following code defines a set of two values for enumerated type sysMode:
classdef(Enumeration) sysMode < int32
enumeration
OFF(0)
ON(1)
end
endAn enumerated type can define any number of values. Each enumerated value consists of a string EnumName and an underlying integer N. Each EnumName must be unique within its type, but can also appear in other enumerated types. The underlying integers need not be either consecutive or ordered, nor must they be unique within the type or across types.
Save the M-file on the MATLAB path.
The name of the M-file must match the name of the enumerated data type. The match is case sensitive.
To add a directory to the MATLAB search path, type addpath pathname at the MATLAB command prompt. For more information, see Using the Search Path, addpath, and savepath in the MATLAB documentation.
For examples of enumerated data type definitions, see Class Definition: sysMode and Class Definition: LEDcolor.
To instantiate an enumerated type in Embedded MATLAB code, use dot notation to specify ClassName.EnumName. For example, the following Embedded MATLAB function displayState instantiates the enumerated types sysMode and LEDcolor from Simple Example: Defining and Using Enumerated Types in Embedded MATLAB Compliant M-Code. The dot notation appears highlighted in the code.
function led = displayState(state)
%#eml
if state == sysMode.ON
led = LEDcolor.GREEN;
else
led = LEDcolor.RED;
endUse the command emlmex to generate C-MEX functions from Embedded MATLAB compliant code that uses enumerated data. Each enumerated data type must be defined on the MATLAB path in a separate M-file as a class derived from the built-in type int32. See How to Define Enumerated Data in Embedded MATLAB Code.
If your Embedded MATLAB function has inputs, you must specify the properties of these inputs to emlmex. For an enumerated data input, use the -eg option to pass one of its allowable values as a sample value. For example, the following emlmex command specifies that the function displayState takes one input of enumerated data type sysMode.
emlmex displayState -eg {sysMode.ON}After executing this command, emlmex generates a platform-specific C-MEX function that you can test in MATLAB. For example, to test displayState, type the following command:
displayState(sysMode.OFF)
You should get the following result:
ans =
REDWorking with Embedded MATLAB MEX to learn more about emlmex
Simple Example: Defining and Using Enumerated Types in Embedded MATLAB Compliant M-Code for a description of the example function displayState and its enumerated type definitions
The following operations work with enumerated operands in the Embedded MATLAB subset. The examples are based on the definitions of the enumeration type LEDcolor described in Class Definition: LEDcolor.
| Example | Result |
|---|---|
xon = LEDcolor.GREEN xoff = LEDcolor.RED | xon =
GREEN
xoff =
RED |
| Example | Result |
|---|---|
xon == xoff | ans =
0 |
xon <= xoff | ans =
1 |
xon > xoff | ans =
0 |
| Example | Result |
|---|---|
double(LEDcolor.RED) | ans =
2 |
z = 2 y = LEDcolor(z) | z =
2
y =
RED |
| Example | Result |
|---|---|
m = [1 2] n = LEDcolor(m) p = n(LEDcolor.GREEN) | m =
1 2
n =
GREEN RED
p =
GREEN |
| Statement | Example | Executable Example |
|---|---|---|
if | if state == sysMode.ON
led = LEDcolor.GREEN;
else
led = LEDcolor.RED;
end | |
switch | switch button
case VCRButton.Stop
state = VCRState.Stop;
case VCRButton.PlayOrPause
state = VCRState.Play;
case VCRButton.Next
state = VCRState.Forward;
case VCRButton.Previous
state = VCRState.Rewind;
otherwise
state = VCRState.Stop;
end | |
while | while state ~= State.Ready
switch state
case State.Standby
initialize();
state = State.Boot;
case State.Boot
boot();
state = State.Ready;
end
end |
The following control statements work with enumerated operands in the Embedded MATLAB subset.
This example is based on the definition of the enumeration types LEDcolor and sysMode. The M-function displayState uses these enumerated data types to activate an LED display.
Class Definition: sysMode. Here is the class definition of the sysMode enumerated data type:
classdef(Enumeration) sysMode < int32
enumeration
OFF(0)
ON(1)
end
endThis definition must reside on the MATLAB path in an M-file with the same name as the class, sysMode.m.
Class Definition: LEDcolor. Here is the class definition of the LEDcolor enumerated data type:
classdef(Enumeration) LEDcolor < int32
enumeration
GREEN(1),
RED(2),
end
endThis definition must reside on the MATLAB path in an M-file called LEDcolor.m.
M-Function: displayState. The following M-function displayState uses enumerated data to activate an LED display, based on the state of a device. It lights a green LED display to indicate the ON state and lights a red LED display to indicate the OFF state.
function led = displayState(state)
%#eml
if state == sysMode.ON
led = LEDcolor.GREEN;
else
led = LEDcolor.RED;
endBuild and Test a C-MEX Executable for displayState.
Generate a C-MEX executable for displayState. Use the -eg option to pass one of the allowable values for the enumerated data input as a sample value.
emlmex displayState -eg {sysMode.ON}Test the function. For example,
displayState(sysMode.OFF)
You should get the following result:
ans =
REDThis example is based on the definition of the enumeration types VCRState and VCRButton. The M-function VCR uses these enumerated data types to set the state of the VCR.
Class Definition: VCRState. Here is the class definition of the VCRState enumerated data type:
classdef(Enumeration) VCRState < int32
enumeration
Stop(0),
Pause(1),
Play(2),
Forward(3),
Rewind(4)
end
end
This definition must reside on the MATLAB path in an M-file with the same name as the class, VCRState.m.
Class Definition: VCRButton. Here is the class definition of the VCRButton enumerated data type:
classdef(Enumeration) VCRButton < int32
enumeration
Stop(1),
PlayOrPause(2),
Next(3),
Previous(4)
end
endThis definition must reside on the MATLAB path in an M-file with the same name as the class, VCRButton.m.
M-Function: VCR. The following M-function VCR uses enumerated data to set the state of a VCR, based on the initial state of the VCR and the state of the VCR button.
function s = VCR(button)
%#eml
persistent state
if isempty(state)
state = VCRState.Stop;
end
switch state
case {VCRState.Stop, VCRState.Forward, VCRState.Rewind}
state = handleDefault(button);
case VCRState.Play
switch button
case VCRButton.PlayOrPause, state = VCRState.Pause;
otherwise, state = handleDefault(button);
end
case VCRState.Pause
switch button
case VCRButton.PlayOrPause, state = VCRState.Play;
otherwise, state = handleDefault(button);
end
end
s = state;
function state = handleDefault(button)
switch button
case VCRButton.Stop, state = VCRState.Stop;
case VCRButton.PlayOrPause, state = VCRState.Play;
case VCRButton.Next, state = VCRState.Forward;
case VCRButton.Previous, state = VCRState.Rewind;
otherwise, state = VCRState.Stop;
end
Build and Test a C-MEX Executable for VCR.
Generate a C-MEX executable for VCR. Use the -eg option to pass one of the allowable values for the enumerated data input as a sample value.
emlmex -eg {VCRButton.Stop} VCRTest the function. For example,
s = VCR(VCRButton.Stop)
You should get the following result:
s =
Stop This example is based on the definition of the enumeration type State. The M-function Setup uses this enumerated data type to set the state of a device.
Class Definition: State. Here is the class definition of the State enumerated data type:
classdef(Enumeration) State < int32
enumeration
Standby(0),
Boot(1),
Ready(2)
end
endThis definition must reside on the MATLAB path in an M-file with the same name as the class, State.m.
M-Function: Setup. The following M-function Setup uses enumerated data to set the state of a device.
function s = Setup(initState)
%#eml
state = initState;
if isempty(state)
state = State.Standby;
end
while state ~= State.Ready
switch state
case State.Standby
initialize();
state = State.Boot;
case State.Boot
boot();
state = State.Ready;
end
end
s = state;
function initialize()
% Perform initialization.
function boot()
% Boot the device.Build and Test a C-MEX Executable for Setup.
Generate a C-MEX executable for Setup. Use the -eg option to pass one of the allowable values for the enumerated data input as a sample value.
emlmex Setup -eg {State.Standby}Test the function. For example,
s = Setup(State.Standby)
You should get the following result:
s =
Ready The following Embedded MATLAB library functions support enumerated types:
Use a unique name for each enumerated data type
Do not use enumerated data as the control variable in switch statements
Do not use enumerated data as the loop counter variable in for- loops
![]() | Generating Code for Variable-Size Data | Working with Structures | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |