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.
Before you can add enumerated data to a Stateflow chart, you must define an enumerated data type in a MATLAB® class definition file. Create a different file for each enumerated type.
The enumerated data type definition consists of three sections of code.
Section of Code | Required? | Purpose |
---|---|---|
classdef | Yes | Provides the name of the enumerated data type |
enumeration | Yes | Lists the enumerated values that the data type allows |
methods | No | Provides methods that customize the data type |
Open a new file in which to store the data type definition. From the Home tab on the MATLAB toolstrip, select New > Class.
Complete the classdef
section of the definition.
classdef BasicColors < Simulink.IntEnumType ... end
The classdef
section defines an enumerated data type with the
name BasicColors
. Stateflow derives the data type from the built-in type
Simulink.IntEnumType
. The enumerated data type name must be unique
among data type names and workspace variable names.
Define enumerated values in an enumeration
section.
classdef BasicColors < Simulink.IntEnumType enumeration Red(0) Yellow(1) Green(2) end end
An enumerated type can define any number of values. The
enumeration
section lists the set of enumerated values that this
data type allows. Each enumerated value consists of a name and an underlying integer
value. Each name must be unique within its type, but can also appear in other enumerated
types. The default value is the first one in the list, unless you specify otherwise in
the methods
section of the definition.
(Optional) Customize the data type by using a methods
section.
The section can contain these methods:
getDefaultValue
specifies a default enumerated value other
than the first one in the list of allowed values.
getDescription
specifies a description of the data type for
code generated by Simulink
Coder™.
getHeaderFile
specifies custom header file that contains
the enumerated type definition in code generated by Simulink
Coder.
getDataScope
enables exporting or importing the enumerated
type definition to or from a header file in code generated by Simulink
Coder.
addClassNameToEnumNames
enhances readability and prevents
name conflicts with identifiers in code generated by Simulink
Coder.
For example, this MATLAB file presents a customized definition for the enumerated data type
BasicColors
that:
Specifies that the default enumerated value is the last one in the list of allowed values.
Includes a short description of the data type for code generated by Simulink Coder.
Imports the definition of the data type from a custom header file to prevent Simulink Coder from generating the definition.
Adds the name of the data type as a prefix to each enumeration member name in code generated by Simulink Coder.
classdef BasicColors < Simulink.IntEnumType enumeration Red(0) Yellow(1) Green(2) end methods (Static = true) function retVal = getDefaultValue() % GETDEFAULTVALUE Specifies the default enumeration member. % Return a valid member of this enumeration class to specify the default. % If you do not define this method, Simulink uses the first member. retVal = BasicColors.Green; end function retVal = getDescription() % GETDESCRIPTION Specifies a string to describe this enumerated type. retVal = 'This defines an enumerated type for colors'; end function retVal = getHeaderFile() % GETHEADERFILE Specifies the file that defines this type in generated code. % The method getDataScope determines the significance of the specified file. retVal = 'imported_enum_type.h'; end function retVal = getDataScope() % GETDATASCOPE Specifies whether generated code imports or exports this type. % Return one of these strings: % 'Auto': define type in model_types.h, or import if header file specified % 'Exported': define type in a generated header file % 'Imported': import type definition from specified header file % If you do not define this method, DataScope is 'Auto' by default. retVal = 'Imported'; end function retVal = addClassNameToEnumNames() % ADDCLASSNAMETOENUMNAMES Specifies whether to add the class name % as a prefix to enumeration member names in generated code. % Return true or false. % If you do not define this method, no prefix is added. retVal = true; end % function end % methods end % classdef
Save the file on the MATLAB path. The name of the file must match exactly the name of the data type.
For example, the definition for the data type BasicColors
must reside
in a file named BasicColors.m
.
Tip
To add a folder to the MATLAB search path, type addpath
at the command prompt.pathname
When you add enumerated data to your chart, specify its type in the Property Inspector.
In the Type field, select Enum: <class
name>
.
Replace <class name>
with the name of the data type. For
example, you can enter Enum: BasicColors
in the
Type field.
(Optional) Enter an initial value for the enumerated data by using a prefixed identifier. The initial value must evaluate to a valid MATLAB expression.