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.
To open the model, at the command prompt, enter:
emldemo_led_switch
The model contains the blocks listed in this table.
Simulink Block | Description |
---|---|
Step | 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 | Converts the |
Data Type Conversion from | Converts the value of type The Data Type Conversion block parameters have these settings:
|
MATLAB Function block | Evaluates the enumerated input |
Display | Displays the value of |
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.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
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
.