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.
View the +SimulinkDemos
data class
package in the folder
(open).matlabroot
/toolbox/simulink/simdemos/dataclasses
This package contains predefined data classes.
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.
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 file Parameter.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
or Signal
,
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
or
Simulink.Signal
.
classdef myParameter < Simulink.Parameter end % classdef
In the class definition, name the constructor method
as myParameter
or mySignal
.
Name the class folder, which contains the class definition,
as @myParameter
or @mySignal
.
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 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
.
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
Use the setupCoderInfo
method to configure
the CoderInfo
object of your class. Then, create
a call to the useLocalCustomStorageClasses
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
Open the Custom Storage Class Designer for your package.
cscdesigner('mypkg')
Define 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
Override the default implementation of the getInstanceSpecificProps
method.
For examples, see CSCTypeAttributes_FlatStructure.m
in
the folder
(open)
and matlabroot
\toolbox\simulink\simulink\dataclasses\+Simulink\@CSCTypeAttributes_FlatStructureCSCTypeAttributes_Unstructed.m
in the folder
(open).matlabroot
\toolbox\simulink\simulink\dataclasses\+mpt\@CSCTypeAttributes_Unstructed
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.
Override the default implementation of the getIdentifiersForInstance
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
If you are using grouped storage classes, override the default
implementation of the getIdentifiersForGroup
method to specify the identifier for the group in generated
code.
For an example, see
CSCTypeAttributes_FlatStructure.m
in the
folder
(open).matlabroot
\toolbox\simulink\simulink\dataclasses\+Simulink\@CSCTypeAttributes_FlatStructure