The following example illustrates how MATLAB Function blocks
exchange enumerated data with other Simulink^{®} blocks. This simple
model uses enumerated data to represent the modes of a device that
controls the colors of an LED display. The MATLAB Function block
receives an enumerated data input representing the mode and, in turn,
outputs enumerated data representing the color to be displayed by
the LED.

This example uses two enumerated types: `switchmode`

to
represent the set of allowable modes and `led`

to
represent the set of allowable colors. Both type definitions inherit
from the built-in type `Simulink.IntEnumType`

. and
must reside on the MATLAB^{®} path.

Here is the class definition of the `switchmode`

enumerated
data type:

classdef switchmode < Simulink.IntEnumType enumeration OFF(0) ON(1) end end

This definition must reside on the MATLAB path in a MATLAB file
with the same name as the class, `switchmode.m`

.

Here is the class definition of the `led`

enumerated
data type:

classdef led < Simulink.IntEnumType enumeration GREEN(1), RED(8) end end

This definition must reside on the MATLAB path in a file
called `led.m`

. The set of allowable values do not
need to be consecutive integers.

Open the example model by typing `emldemo_led_switch`

at
the MATLAB command prompt. The model contains the following blocks:

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 `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` . In the Data
Type Conversion block, you specify the enumerated data type
using the prefix Output minimum: `[]` Output maximum: `[]` Output data type: `Enum:switchmode`
For more information about specifying enumerated types in Simulink models, see Define Enumerated Data Types for MATLAB Function Blocks. |

MATLAB Function | Evaluates enumerated data input `state` to
determine the color to output as enumerated data `ledval` .
See MATLAB Function Block: checkState. |

Display | Displays the enumerated value of output `led` . |

The function `checkState`

in the MATLAB
Function block uses enumerated data to activate an LED display,
based on the state of a device. It lights a green LED display to indicate
the ON state and 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

The input `state`

inherits its enumerated type `switchmode`

from
the Simulink step signal; the enumerated type of output `ledval`

is
explicitly declared as `Enum:led`

:

Explicit enumerated type declarations must include the prefix `Enum:`

.
For more information, see Define Enumerated Data Types for MATLAB Function Blocks.

Wherever possible, Simulink displays the name of an enumerated value, not its underlying integer. For instance, Display blocks display the name of enumerated values. In this example, when the model simulates for less than 10 seconds, the step signal is 0, resulting in a red LED display to signify the off state.

Similarly, if the model simulates for 10 seconds or more, the step signal is 1, resulting in a green LED display to signify the on state.

Simulink scope blocks work differently. For more information, see Enumerations and Scopes.

Was this topic helpful?