| Contents | Index |
The elements of an enumerated data type definition appear as follows:
classdef(Enumeration) BasicColors < Simulink.IntEnumType
enumeration
Red(0)
Yellow(1)
Green(2)
end
methods (Static = true)
function retVal = getDefaultValue()
% GETDEFAULTVALUE Returns the default enumerated value.
% This value must be an instance of the enumerated type.
% Used by Simulink when an instance of this class is
% needed but the value is not known (e.g., initializing
% ground values or casting an invalid numeric value to
% an enumerated type). If this method is not defined,
% the first value is used.
retVal = BasicColors.Green;
end
function retVal = getDescription()
% GETDESCRIPTION Optional string to describe data type.
retVal = 'This defines an enumerated type for colors';
end
function retVal = getHeaderFile()
% GETHEADERFILE File where type is defined for generated
% code. If specified, this file is #included as needed
% in the code. Otherwise, the type is written out in
% the generated code.
retVal = 'imported_enum_type.h';
end
function retVal = addClassNameToEnumNames()
% ADDCLASSNAMETOENUMNAMES Specify if class name is added
% as a prefix to enumerated names in the generated code.
% By default we do not add the prefix.
retVal = true;
end
end
endThe data type definition consists of three sections of code.
| Section of Code | Required? | Purpose | Reference |
|---|---|---|---|
| classdef | Yes | Gives 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:
| Enumerations and Modeling in the Simulink User's Guide Enumerations in the Simulink Coder User's Guide |
In the example, the methods section of code customizes the data type as follows:
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 Simulink Coder generated code
Uses a custom header file to prevent the data type from being written out in Simulink Coder generated code
Adds the name of the data type as a prefix to each enumerated name in Simulink Coder generated code
![]() | Where to Use Enumerated Data | How to Define Enumerated Data in a Stateflow Chart | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |