| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Real-Time Workshop |
| Contents | Index |
| Learn more about Real-Time Workshop |
Every enumerated class has four associated static methods, which it inherits from Simulink.IntEnumType. You can optionally override any or all of these static methods to customize the behavior of an enumerated type. The methods are:
getDefaultValue — Returns the default value of the enumerated data type.
getDescription — Returns a description of the enumerated data type.
getHeaderFile — Specifies a file where the type is defined for generated code.
addClassNameToEnumNames — Specifies whether the class name becomes a prefix in code.
The first of these methods, getDefaultValue, is relevant to both simulation and code generation, and is described in Specifying a Default Enumerated Value in the Simulink documentation. The other three methods are relevant only to code generation, and are described in this section. To override any of the methods, include a customized version of the method in the enumerated class definition's methods section. If you do not want to override any default methods, omit the methods section entirely. The following table summarizes the four methods and the data to supply for each one:
| Method | Purpose | Default Return | Custom Return |
|---|---|---|---|
| getDefaultValue | Returns the default value for the class, which must be an instance of the class. | The lexically first value in the enumeration. | Any enumerated value in the class. See Instantiating an Enumerated Type. |
| getDescription | Returns a string containing a description of the enumerated class. | '' | Any string that MATLAB accepts. |
| getHeaderFile | Returns a string containing the name of the header file | '' | The name of the file that contains the enumerated type definition. |
| addClassNameToEnumNames | Returns a boolean value indicating whether to prefix the class name in generated code | false | true or false |
To specify a description for an enumerated data type, include the following method in the enumerated class's methods section:
function retVal = getDescription()
% GETDESCRIPTION Optional string to describe the data type.
retVal = 'description';
endSubstitute any legal MATLAB string for description. The generated code that defines the enumerated type will include the specified description.
To prevent the declaration of an enumerated type from being embedded in the generated code, allowing you to provide the declaration in an external file, include the following method in the enumerated class's methods section:
function retVal = getHeaderFile()
% GETHEADERFILE File where type is defined for generated code.
% If specified, this file is #included where needed in the code.
% Otherwise, the type is written out in the generated code.
retVal = 'filename';
endSubstitute any legal filename for filename. Be sure to provide a filename suffix, typically .h. Providing the method replaces the declaration that would otherwise have appeared in model_types.h with a #include statement like:
#include "imported_enum_type.h"
The getHeaderFile method does not create the declaration file itself. You must provide a file of the specified name that declares the enumerated data type.
By default, enumerated values in generated code have the same names that they have in the enumerated class definition. Alternatively, the code can prefix every enumerated value in an enumerated class with the name of the class. This technique can be useful for preventing identifier conflicts or improving the clarity of the code. To specify class name prefixing, include the following method in an enumerated class's methods section:
function retVal = addClassNameToEnumNames()
% ADDCLASSNAMETOENUMNAMES Control whether class name is added as
% a prefix to enumerated names in the generated code.
% By default the code does not use the class name as a prefix.
retVal = boolean;
endReplace boolean with true to enable class name prefixing, or false to suppress prefixing without having to delete the method itself. If boolean is true, each enumerated value in the class appears in generated code as EnumTypeName_EnumName. For BasicColors, which was defined in About Enumerated Data Types, the data type definition with class name prefixing looks like this:
#ifndef _DEFINED_TYPEDEF_FOR_BasicColors_
#define _DEFINED_TYPEDEF_FOR_BasicColors_
typedef enum {
BasicColors_Red = 0, /* Default value */
BasicColors_Yellow = 1,
BasicColors_Blue = 2,
} BasicColors;
#endifIn this example, the enumerated class name BasicColors appears as a prefix for each of the enumerated names. The definition is otherwise the same as it would be without name prefixing.
![]() | Enumerated Type Safe Casting | How emlc Works with Enumerated Types | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |