Main Content

Simulink Enumerations

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®.

ExampleShows How To Use...
Data Typing in SimulinkData types in Simulink, including enumerated data types
Simulate a Media Player (Stateflow)Enumerated data types in a Simulink model that contains a Stateflow chart

For information on using enumerations in Stateflow, see Enumerated Data (Stateflow).

Simulink Constructs that Support Enumerations

Overview

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, port value labels, 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.

Block Support

Many Simulink blocks support enumerated types. The Data Type Support section of each block reference page describes all data types that the block supports.

In particular, all members of the following categories of Simulink blocks support enumerated types:

Class Support

The following Simulink classes support enumerated types:

  • Simulink.ValueType

  • Simulink.Signal

  • Simulink.Parameter

  • Simulink.AliasType

  • Simulink.BusElement

Logging Enumerated Data

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. See Save Run-Time Data from Simulation for more information.

Importing Enumerated Data

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. See Load Signal Data for Simulation for more information.

Simulink Enumeration Limitations

Enumerations and Scopes

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.

Enumerated Types for Switch Blocks

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.

Nonsupport of Enumerations

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.

See Also

| |

Related Topics