Use Enumerations to Control an LED Display

This example shows how to use enumerations in a MATLAB Function block. The example shows how MATLAB Function blocks exchange enumerated data with other Simulink® blocks.

The emldemo_led_switch model uses enumerations to represent the modes of a device that controls the colors of an LED display. The MATLAB Function block receives an enumerated input signal that represents the mode. The enumerated output signal represents the color that the LED displays.

Simulink Model

To open the model, at the command prompt, enter:


The model contains the blocks listed in this table.

Simulink BlockDescription


Provides source of the on/off signal. Outputs an initial value of 0 (off) and at 10 seconds steps up to a value of 1 (on).

Data Type Conversion from double to int32

Converts the Step signal of type double to type int32.

Data Type Conversion from int32 to enumerated type switchmode

Converts the value of type int32 to the enumerated type switchmode.

The Data Type Conversion block parameters have these settings:

  • Output minimum: []

  • Output maximum: []

  • Output data type: Enum:switchmode

MATLAB Function block checkState

Evaluates the enumerated input state to determine the value of the enumerated output ledval. state inherits its enumerated type switchmode from the Simulink step signal. ledval has the type Enum:led.


Displays the value of ledval.

Enumeration Class Definitions

The switchmode enumeration represents the allowed modes for the input to the checkstate block.

classdef switchmode < Simulink.IntEnumType

The led enumeration represents the colors that the checkstate block can output.

classdef led < Simulink.IntEnumType
Both enumeration classes inherit from the built-in type Simulink.IntEnumType and reside on the MATLAB® path.

MATLAB Function Block Function

The function checkState uses enumerations to activate an LED display, based on the state of a device. It lights a green LED display to indicate the ON state. It lights a red LED display to indicate the OFF state.

function ledval = checkState(state)

if state == switchmode.ON
    ledval = led.GREEN;
    ledval = led.RED;


When you simulate the model, the Display block displays the state of the LED display. If you simulate the model for less than 10 seconds, the state is off. The Display block displays RED. If you simulate the model for more than 10 seconds, the state is on. The Display block displays GREEN.

Related Topics