此示例说明如何创建 Simulink® 数据类的子类。
使用 MATLAB® 类语法在一个包中创建一个数据类。或者,为数据类指定属性并定义存储类。
查看
文件夹中的数据类包 matlabroot
/toolbox/simulink/simdemos/dataclasses+SimulinkDemos
(打开)。
该包包含预定义的数据类。
将该文件夹复制到您要用于定义您的数据类的位置。
重命名文件夹 +mypkg
并将其父文件夹添加到 MATLAB 路径中。
修改数据类定义。
创建包文件夹 +mypkg
并将其父文件夹添加到 MATLAB 路径中。
在 +mypkg
内创建类文件夹 @Parameter
和 @Signal
。
注意
Simulink 要求在 +Package/@Class
文件夹内定义数据类。
在 @Parameter
文件夹中,创建 MATLAB 文件 Parameter.m
并将其打开进行编辑。
使用 MATLAB 类语法定义作为 Simulink.Parameter
子类的数据类。
classdef Parameter < Simulink.Parameter end % classdef
要使用自定义类名称而不是 Parameter
或 Signal
,请使用自定义名称命名类文件夹。例如,要定义一个类 mypkg.myParameter
:
将数据类定义为 Simulink.Parameter
或 Simulink.Signal
的子类。
classdef myParameter < Simulink.Parameter end % classdef
在类定义中,将构造函数方法命名为 myParameter
或 mySignal
。
将类文件夹(其中包含类定义)命名为 @myParameter
或 @mySignal
。
属性定义模块的前后分别用 properties
和 end
关键字括起来。
classdef Parameter < Simulink.Parameter properties % Unconstrained property type Prop1 = []; end properties(PropertyType = 'logical scalar') Prop2 = false; end properties(PropertyType = 'char') Prop3 = ''; end properties(PropertyType = 'char',... AllowedValues = {'red'; 'green'; 'blue'}) Prop4 = 'red'; end end % classdef
如果要将属性添加到 Simulink.Parameter
、Simulink.Signal
或 Simulink.CustomStorageClassAttributes
的子类,则可以指定以下属性类型。
属性类型 | 语法 |
---|---|
双精度值 | properties(PropertyType = 'double
scalar') |
int32 值 | properties(PropertyType = 'int32
scalar') |
逻辑值 | properties(PropertyType = 'logical
scalar') |
字符向量 (char) | properties(PropertyType =
'char') |
具有有限个允许值的字符向量 | properties(PropertyType = 'char', AllowedValues
= {'a', 'b', 'c'}) |
如果使用 MATLAB 属性验证(请参阅验证属性值),而不是 PropertyType
,则这些属性将在该类的属性对话框中显示为编辑字段。如果您使用 PropertyType
和 AllowedValues
,则会显示属性对话框:
逻辑标量属性的复选框。
字符向量和 AllowedValues
的下拉菜单。
您可以将构造函数添加您的数据类中以在实例化该类时执行初始化活动。添加的构造函数不能要求输入参数。
在此示例中,构造函数基于可选的输入参数初始化对象 obj
的值。
classdef Parameter < Simulink.Parameter methods function obj = Parameter(optionalValue) if (nargin == 1) obj.Value = optionalValue; end end end % methods end % classdef
使用 setupCoderInfo
方法来配置您的类的 CoderInfo
对象。然后,创建对 useLocalCustomStorageClasses
方法的调用并打开 Custom Storage Class Designer。
在您的数据类内的构造函数中,调用 useLocalCustomStorageClasses
方法。
classdef Parameter < Simulink.Parameter methods function setupCoderInfo(obj) useLocalCustomStorageClasses(obj, 'mypkg'); obj.CoderInfo.StorageClass = 'Custom'; end end % methods end % classdef
为您的包打开 Custom Storage Class Designer。
cscdesigner('mypkg')
定义存储类。
创建 MATLAB 文件 myCustomAttribs.m
并将其打开进行编辑。将此文件保存在 +mypkg/@myCustomAttribs
文件夹中,其中 +mypkg
是包含 @Parameter
和 @Signal
文件夹的文件夹。
使用 MATLAB 类语法定义 Simulink.CustomStorageClassAttributes
的子类。例如,考虑一个存储类,它不仅使用原始标识符定义数据,还在生成的代码中提供数据的替代名称。
classdef myCustomAttribs < Simulink.CustomStorageClassAttributes properties(PropertyType = 'char') AlternateName = ''; end end % classdef
覆盖 isAddressable
方法的默认实现以确定存储类是否可写。
classdef myCustomAttribs < Simulink.CustomStorageClassAttributes properties(PropertyType = 'logical scalar') IsAlternateNameInstanceSpecific = true; end methods function retVal = isAddressable(hObj, hCSCDefn, hData) retVal = false; end end % methods end % classdef
覆盖 getInstanceSpecificProps
方法的默认实现。
有关示例,请参阅文件夹
(打开)中的 matlabroot
\toolbox\simulink\simulink\dataclasses\+Simulink\@CSCTypeAttributes_FlatStructureCSCTypeAttributes_FlatStructure.m
和文件夹
(打开)中的 matlabroot
\toolbox\simulink\simulink\dataclasses\+mpt\@CSCTypeAttributes_UnstructedCSCTypeAttributes_Unstructed.m
。
注意
这是可选步骤。默认情况下,所有自定义属性都特定于实例且对于每个数据对象来说都是可修改的。但是,您可以限制哪些属性可以是特定于实例的。
覆盖 getIdentifiersForInstance
方法的默认实现以定义数据类的对象的标识符。
注意
在其默认实现中,此方法查询数据对象的名称或标识符,并在生成的代码中使用该标识符。通过覆盖此方法,您可以控制生成的代码中您的数据对象的标识符。
classdef myCustomAttribs < Simulink.CustomStorageClassAttributes properties(PropertyType = 'char') GetFunction = ''; SetFunction = ''; end methods function retVal = getIdentifiersForInstance(hCSCAttrib,... hCSCDefn, hData, identifier) retVal = struct('GetFunction',... hData.CoderInfo.CustomAttributes.GetFunction, ... 'SetFunction', hData.CoderInfo.CustomAttributes.SetFunction); end% end % methods end % classdef
如果您正在使用已分组的存储类,请覆盖 getIdentifiersForGroup
方法的默认实现以指定在生成的代码中该组的标识符。
有关示例,请参阅文件夹
中的 matlabroot
\toolbox\simulink\simulink\dataclasses\+Simulink\@CSCTypeAttributes_FlatStructureCSCTypeAttributes_FlatStructure.m
(打开)。