Enumerated data is data that is restricted to a finite set of values. An enumerated data type is a MATLAB® class that defines a set of enumerated values. Each enumerated value consists of an enumerated name and an underlying integer which the software uses internally and in generated code.
Before you begin to use enumerations in a modeling context, you should understand information provided in Enumerations.
To define an enumeration for use in Simulink® models, choose one of these techniques:
Use the function Simulink.defineIntEnumType
.
The enumeration exists for the duration of your MATLAB session.
Create a permanent enumeration class by subclassing one of these built-in classes:
Many of the built-in integer data types such as int8
and uint16
Simulink.IntEnumType
Use the function Simulink.importExternalCTypes
to
create a Simulink representation of an enumerated data type (enum
)
that your external C code defines.
Use this technique to help you:
Replace existing C code with a Simulink model.
Integrate existing C code for simulation in Simulink (for example, by using the Legacy Code Tool).
Generate C code (Simulink Coder™) that you can compile with existing C code into a single application.
For more information, see Define Simulink Enumerations.
The following examples show how to use enumerations in Simulink and Stateflow®.
Example | Shows How To Use... |
---|---|
Data Typing in Simulink | Data types in Simulink, including enumerated data types |
Modeling a CD Player/Radio Using Enumerated Data Types | Enumerated data types in a Simulink model that contains a Stateflow chart |
For information on using enumerations in Stateflow, see Enumerated Data (Stateflow).
In general, all Simulink tools and constructs support enumerated types for which the support makes sense given the purpose of enumerated types: to represent program states and to control program logic. The Simulink Editor, Simulink Debugger, Port Value Displays, referenced models, subsystems, masks, buses, data logging, and most other Simulink capabilities support enumerated types without imposing any special requirements.
Enumerated types are not intended for mathematical computation, so no block that computes a numeric output (as distinct from passing a numeric input through to the output) supports enumerated types. Thus an enumerated type is not considered to be a numeric type, even though an enumerated value has an underlying integer. See Enumerated Values in Computation for more information.
Most capabilities that do not support enumerated types obviously could not support them. Therefore, the Simulink documentation usually mentions enumerated type nonsupport only where necessary to prevent a misconception or describe an exception. See Simulink Enumeration Limitations for information about certain constructs that could support enumerated types but do not.
The following Simulink blocks support enumerated types:
Constant (but Enumerated Constant is preferable)
Probe (input only)
Relay (output only)
All members of the following categories of Simulink blocks support enumerated types:
Bus-capable blocks (see Bus-Capable Blocks)
Pass-through blocks:
With state, like the Data Store Memory and Unit Delay blocks.
Without state, like the Mux block.
Many Simulink blocks in addition to those named above support enumerated types, but they either belong to one of the categories listed above, or are rarely used with enumerated types. The Data Type Support section of each block reference page describes all data types that the block supports.
The following Simulink classes support enumerated types:
Simulink.Signal
Simulink.Parameter
Simulink.AliasType
Simulink.BusElement
Top-level model output ports, To Workspace blocks, and Scope blocks can all export enumerated values. Signal and State logging work with enumerated data in the same way as with any other data. All logging formats are supported. The From File block does not support enumerated data. Use the From Workspace block instead, combined with some technique for transferring data between a file and a workspace. See Save Run-Time Data from Simulation for more information.
Top-level model input ports and From Workspace blocks can output enumerated
signals during simulation. Data must be provided in a Structure
,
Structure with Time
, or TimeSeries
object. No
interpolation occurs for enumerated values between the specified simulation times.
From File blocks produce only data of type double
, so
they do not support enumerated types. See Load Signal Data for Simulation for more
information.
When a Scope block displays an enumerated signal, the vertical axis displays the names of the enumerated values only if the scope was open during simulation. If you open the Scope block for the first time before any simulation has occurred, or between simulations, the block displays only numeric values. When simulation begins, enumerated names replace the numeric values, and thereafter appear whenever the Scope block is opened.
When a Floating Scope block displays multiple signals, the names of enumerated values appear on the Y axis only if all signals are of the same enumerated type. If the Floating Scope block displays more than one type of enumerated signal, or any numeric signal, no names appear, and any enumerated values are represented by their underlying integers.
The control input of a Switch block
can be of any data type supported by Simulink. However, the u2
~=0
mode is not supported for enumerations. If the control
input has an enumeration, choose one of the following methods to specify
the criteria for passing the first input:
Select u2 >= Threshold
or u2
> Threshold
and specify a threshold value of the
same enumerated type as the control input.
Use a Relational Operator block to do the comparison and then feed the Boolean result of this comparison into the control port of the Switch block.
The following limitations exist when using enumerated data types with Simulink:
Packages cannot contain enumeration class definitions.
The If Action block does not support enumerations.
Generated code does not support logging enumerated data.
Custom Stateflow targets do not support enumerated types.
enumeration
| Simulink.data.getEnumTypeInfo
| Simulink.defineIntEnumType