To enhance the readability of a Stateflow® chart, use enumerated data. With enumerated data, you can:
Create a restricted set of values and refer to those values by name.
Group related values into separate data types.
Avoid defining a long list of constants.
Enumerated data is supported in Stateflow charts in Simulink® models. For more information, see Reference Values by Name by Using Enumerated Data.
This example shows how to build a chart that uses enumerated values to issue a status keyword.
During simulation, the chart action alternates between states A
and
B
.
A
At the start of the simulation, state A
is entered.
State A
executes the entry
action by
assigning the value RED
to the enumerated data
color
.
The data y
increments once per time step (every 0.2 seconds)
until the condition [y > 6]
is true.
The chart takes the transition from state A
to state
B
.
B
After the transition from state A
occurs, state
B
is entered.
State B
executes the entry
action by
assigning the value GREEN
to the enumerated data
color
.
The data y
decrements once per time step (every 0.2 seconds)
until the condition [y < 3]
is true.
The chart takes the transition from state B
back to state
A
.
To create a Simulink model with an empty chart, at the MATLAB® command prompt, enter sfnew
.
In the empty chart, add states A
and B
. At
the text prompt, enter the appropriate action statements.
Add a default transition to state A
and transitions between
states A
and B
.
Double-click each transition. At the text prompt, enter the appropriate condition.
To create a file in which to store the data type definition, from the Home tab on the MATLAB toolstrip, select New > Class.
In the MATLAB Editor, enter:
classdef TrafficColors < Simulink.IntEnumType enumeration RED(0) GREEN(10) end end
classdef
section defines an integer-based enumerated data type
named TrafficColors
. The enumeration
section
contains the enumerated values that this data type allows followed by their underlying
numeric value.Save your file as TrafficColors.m
in a folder on the
MATLAB search path.
To resolve the undefined data, in the Symbols pane, click the Resolve
undefined symbols icon . The Stateflow Editor assigns an appropriate scope to each symbol in the
chart.
Symbol | Scope |
---|---|
color | Output Data |
y | Local Data |
GREEN | Parameter Data |
RED | Parameter Data |
To specify color
as enumerated data, in the Property Inspector:
In the Type field, select Enum: <class
name>
. Replace <class name>
with
TrafficColors
, the name of the data type that you defined
previously.
Under Logging, select the Log signal data check box.
To set the scope and type of y
, in the Property Inspector:
In the Scope field, select
Output
.
In the Type field, select
uint8
.
Under Logging, select the Log signal data check box.
In the Symbols pane, delete the symbols GREEN
and
RED
. The Stateflow Editor incorrectly identified these symbols as parameters before you
specified color
as enumerated data.
When you simulate the model, the Simulation Data Inspector
icon is highlighted to indicate that it has new simulation data. To
open the Simulation Data Inspector, click the icon.
In the Simulation Data Inspector, select the check boxes for the
color
and y
signals so that they are displayed
on separate axes.
To access the logged data in the MATLAB workspace, call the signal logging object logsout
.
For example, at the command prompt, enter:
losgout = out.logsout; colorLog = logsout.getElement('color'); Tbl = table(colorLog.Values.Time,colorLog.Values.Data); Tbl.Properties.VariableNames = {'SimulationTime','Color'}
Tbl = 9×2 table SimulationTime Color ______________ _____ 0 RED 1.6 GREEN 2.8 RED 4 GREEN 5.2 RED 6.4 GREEN 7.6 RED 8.8 GREEN 10 RED