Use Enumerations to Control an LED Display
Enumerations in a MATLAB Function Block
This example shows how MATLAB Function blocks exchange enumerated data with other Simulink® blocks. The 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.
In this model, the Step
block provides the source of the on/off signal. This block outputs an initial value of 0 (off) and, after 10 seconds, steps up to a value of 1 (on). A pair of Data Type Conversion blocks convert this signal, first from type double
to type int32
, and then from int32
to the enumerated type switchmode
. The parameters in the second Data Type Conversion block have these settings:
Output minimum:
[]
Output maximum:
[]
Output data type:
Enum:switchmode
The 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 incoming Simulink signal, while ledval
has the type Enum:led
. Finally, a Display block 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 enumeration OFF(0) ON(1) end end
The led
enumeration represents the colors that the
checkstate
block can output.
classdef led < Simulink.IntEnumType enumeration GREEN(1), RED(8) end end
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) %#codegen if state == switchmode.ON ledval = led.GREEN; else ledval = led.RED; end
Simulation
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
.