定义枚举数据类型
要增强 Stateflow® 图的可读性,请使用枚举数据。使用枚举数据,您可以:
创建一组有限的值,并按名称引用这些值。
将相关值组合为单独的数据类型。
避免定义冗长的常量列表。
在 Simulink® 模型的 Stateflow 图中支持枚举数据。有关详细信息,请参阅Reference Values by Name by Using Enumerated Data。
必须在 MATLAB® 类定义文件中定义枚举数据类型,才能将该枚举数据添加到 Stateflow 图。为每个枚举类型创建一个不同文件。
枚举数据类型定义的元素
枚举数据类型定义由三个代码节组成。
代码节 | 必需项? | 目的 |
---|---|---|
classdef | 是 | 提供枚举数据类型的名称 |
enumeration | 是 | 列出数据类型允许的枚举值 |
methods | 否 | 提供自定义数据类型的方法 |
定义枚举数据类型
打开存储数据类型定义的新文件。从 MATLAB 工具条上的主页选项卡中,选择新建 > 类。
完成定义的
classdef
节。classdef BasicColors < Simulink.IntEnumType ... end
classdef
节定义了一个名为BasicColors
的枚举数据类型。Stateflow 从内置类型Simulink.IntEnumType
中派生该数据类型。枚举数据类型的名称在数据类型名称和工作区变量名称中必须唯一。在
enumeration
节中定义枚举值。classdef BasicColors < Simulink.IntEnumType enumeration Red(0) Yellow(1) Green(2) end end
一个枚举类型可以定义任意数量的值。
enumeration
节列出此数据类型允许的一组枚举值。每个枚举值由一个名称和一个基础整数值组成。每个名称在其类型中必须唯一,但也可以出现在其他枚举类型中。默认值为列表中的第一个值,除非您在定义的methods
节中另行指定。(可选)使用
methods
节自定义数据类型。该节可以包含以下方法:getDefaultValue
指定除允许值列表中第一个值以外的默认枚举值。getDescription
指定由 Simulink Coder™ 生成的代码的数据类型的描述。getHeaderFile
指定自定义头文件,该头文件包含由 Simulink Coder 生成的代码中的枚举类型定义。getDataScope
允许将枚举类型定义导出至由 Simulink Coder 生成的代码的头文件中或从中导入。addClassNameToEnumNames
增强了可读性,并防止与 Simulink Coder 生成的代码中的标识符发生名称冲突。
例如,此 MATLAB 文件表示枚举数据类型
BasicColors
的自定义定义,该定义:指定默认枚举值是允许值列表中的最后一个值。
包括由 Simulink Coder 生成的代码的数据类型的简短描述。要在生成的代码中包含描述,请启用 Simulink 数据对象描述 (Embedded Coder)模型配置参数。此参数需要 Embedded Coder® 许可证。
从自定义头文件导入数据类型的定义,以防止 Simulink Coder 生成该定义。
将数据类型的名称作为前缀添加到由 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
将文件保存在 MATLAB 路径下。文件名必须与数据类型的名称完全匹配。例如,数据类型
BasicColors
的定义必须位于名为BasicColors.m
的文件中。提示
要将文件夹添加到 MATLAB 搜索路径中,请在命令提示符下键入
addpath
。pathname
在属性检查器中指定数据类型
向图中添加枚举数据时,请在属性检查器中指定其类型。
在类型字段中,选择 Enum: <class name>。
用数据类型的名称替换
<class name>
。例如,您可以在类型字段中输入 Enum:BasicColors。(可选)使用带前缀的标识符输入枚举数据的初始值。初始值的计算结果必须为有效的 MATLAB 表达式。有关带前缀和不带前缀的标识符的详细信息,请参阅Notation for Enumerated Values。