Define Data Classes - MATLAB & Simulink (original) (raw)
This example shows how to subclass SimulinkĀ® data classes.
Use MATLABĀ® class syntax to create a data class in a package. Optionally, assign properties to the data class and define storage classes.
- Use an example to define data classes
- Manually define data class
- Optional: Add properties to data class
- Optional: Add initialization code to data class
- Optional: Define storage classes
- Optional: Define custom attributes for storage classes
Use an example to define data classes
- In the working folder, view the
+SimulinkDemos
data class package in the folderdataclasses
. - Copy the folder to the location where you want to define your data classes.
- Rename the folder
+mypkg
and add its parent folder to the MATLAB path. - Modify the data class definitions.
Manually define data class
- Create a package folder
+mypkg
and add its parent folder to the MATLAB path. - Create class folders
@Parameter
and@Signal
inside+mypkg
.
Note
Simulink requires data classes to be defined inside+Package/@Class
folders. - In the
@Parameter
folder, create a MATLAB fileParameter.m
and open it for editing. - Define a data class that is a subclass of
Simulink.Parameter
using MATLAB class syntax.
classdef Parameter < Simulink.Parameter
end % classdef
To use a custom class name other than Parameter
orSignal
, name the class folders using the custom name. For example, to define a class mypkg.myParameter
:
- Define the data class as a subclass of
Simulink.Parameter
orSimulink.Signal
.
classdef myParameter < Simulink.Parameter
end % classdef
- In the class definition, name the constructor method as
myParameter
ormySignal
. - Name the class folder, which contains the class definition, as
@myParameter
or@mySignal
.
Optional: Add properties to data class
The properties
and end
keywords enclose a property definition block.
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
If you add properties to a subclass of Simulink.Parameter
,Simulink.Signal
, or Simulink.CustomStorageClassAttributes
, you can specify the following property types.
Property Type | Syntax |
---|---|
Double number | properties(PropertyType = 'double scalar') |
int32 number | properties(PropertyType = 'int32 scalar') |
Logical number | properties(PropertyType = 'logical scalar') |
Character vector (char) | properties(PropertyType = 'char') |
Character vector with limited set of allowed values | properties(PropertyType = 'char', AllowedValues = {'a', 'b', 'c'}) |
If you add a property that requires special copy behavior, you define that behavior by overriding the copyElement
method. For example, in the +SimulinkDemos
data class package, the class definition fileParameter.m
defines the propertyGenericProperty
with an unconstrained property type. ThecopyElement
method specifies the copy behavior forGenericProperty
. When you add, remove, or change a property that requires special copy behavior, you must also be sure to update thecopyElement
method.
If you use MATLAB property validation (see Validate Property Values) instead of PropertyType
, the properties are displayed as an edit field in the property dialog box of the class. If you use PropertyType
and AllowedValues
, then the property dialog box displays:
- A check box for logical scalar properties.
- A dropdown menu for character vectors and
AllowedValues
.
Optional: Add initialization code to data class
You can add a constructor within your data class to perform initialization activities when the class is instantiated. The added constructor cannot require an input argument.
In this example, the constructor initializes the value of object obj
based on an optional input argument.
classdef Parameter < Simulink.Parameter methods function obj = Parameter(optionalValue) if (nargin == 1) obj.Value = optionalValue; end end end % methods end % classdef
Optional: Define storage classes
Use the setupCoderInfo
method to configure theCoderInfo
object of your class. Then, create a call to theuseLocalCustomStorageClasses
method and open the Custom Storage Class Designer.
- In the constructor within your data class, call the
useLocalCustomStorageClasses
method.
classdef Parameter < Simulink.Parameter
methods
function setupCoderInfo(obj)
useLocalCustomStorageClasses(obj, 'mypkg');
obj.CoderInfo.StorageClass = 'Custom';
end
end % methods
end % classdef 2. Open the Custom Storage Class Designer for your package. 3. Define storage classes.
Optional: Define custom attributes for storage classes
- Create a MATLAB file
myCustomAttribs.m
and open it for editing. Save this file in the+mypkg/@myCustomAttribs
folder, where+mypkg
is the folder containing the@Parameter
and@Signal
folders. - Define a subclass of
Simulink.CustomStorageClassAttributes
using MATLAB class syntax. For example, consider a storage class that defines data using the original identifier but also provides an alternate name for the data in generated code.
classdef myCustomAttribs < Simulink.CustomStorageClassAttributes
properties(PropertyType = 'char')
AlternateName = '';
end
end % classdef - Override the default implementation of the
isAddressable
method to determine whether the storage class is writable.
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
4. Override the default implementation of thegetInstanceSpecificProps
method.
For example, see these override functions:
function props = getInstanceSpecificProps(hObj)
% GETINSTANCESPECIFICPROPERTIES Return instance-specific properties
% (custom attributes that can be modified on each data object).
if hObj.IsStructNameInstanceSpecific
props = findprop(hObj, 'StructName');
else
props = [];
end
end
function props = getInstanceSpecificProps(hObj)
% GETINSTANCESPECIFICPROPERTIES Return instance-specific properties
% (custom attributes that can be modified on each data object).
props = [];
if hObj.IsOwnerInstanceSpecific
ptmp = findprop(hObj, 'Owner');
props = [props; ptmp];
end
if hObj.IsDefinitionFileInstanceSpecific
ptmp = findprop(hObj, 'DefinitionFile');
props = [props; ptmp];
end
if hObj.IsPersistenceLevelInstanceSpecific
ptmp = findprop(hObj, 'PersistenceLevel');
props = [props; ptmp];
end
end
Note
This is an optional step. By default, all custom attributes are instance-specific and are modifiable for each data object. However, you can limit which properties are allowed to be instance-specific.
5. Override the default implementation of thegetIdentifiersForInstance
method to define identifiers for objects of the data class.
Note
In its default implementation, this method queries the name or identifier of the data object and uses that identifier in generated code. By overriding this method, you can control the identifier of your data objects in generated code.
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
6. If you are using grouped storage classes, override the default implementation of thegetIdentifiersForGroup
method to specify the identifier for the group in generated code.
For an example, in the working folder, seeCSCTypeAttributes_FlatStructure.m
in the folder@CSCTypeAttributes_FlatStructure
.