Using Simulink | ![]() ![]() |
Creating Data Object Classes
Creating a new data object class entails writing M-file programs to construct and instantiate instances of the class. If you want to create a new package to contain the class, you must also write an M-file constructor for the new package.
Package Directory Structure
You must store the programs that define a class in a directory that has a prescribed structure.
The directory structure must mee tthe following requirements.
@PackageName
where PackageName
is the name of the package. @ClassName
where ClassName
is the name of the new class.The package directory must contain an M-file program, named schema.m
, that constructs the package. Each class directory must contain a constructor, named schema.m
, and an instantiation function, named ClassName.m
, where ClassName
is the name of the class.
Creating a Package
To create a package, first create a directory named @package_name
in a directory on the MATLAB path, where @PackageName
is the name of the new package. Then create a M-file named schema.m
in the package directory. The schema.m file MATLAB function.
function schema () % Package constructor function schema.package('PackageName');
where PackageName
is the name of the new package.
Creating a Class
To create a data object class,
@ClassName
, where ClassName
is the name of the new class, in the directory of the package in which you want the new class to reside.
Creating a Class Constructor
MATLAB finds the constructor for a class by looking for a function named schema in the class directory. You must therefore create this function in the class directory of the class you are creating.The constructor creates the class by invoking the create_user_class
function (see create_user_class) as illustrated in the following example.
function schema() % Class constructor function. % Specify name of class to be created: userClass = 'UserDefined.Parameter'; % Specify name of class from which user class is derived: deriveFromClass = 'Simulink.Parameter'; % Call generalized constructor function for % user-defined enumerations used by this class create_user_enumtype('colors', {'red', 'green', 'blue'}); % Specify new properties to include in user class: addProperties = { 'UserMATLABArray1', 'MATLAB array', []; ... 'UserMATLABArray2', 'MATLAB array', ''; ... 'UserDouble', 'double', 0; ... 'UserInt32', 'int32', 0; ... 'UserOnOff', 'on/off', 'off'; ... 'UserString', 'string', ''; ... 'UserColorEnum', 'colors', 'red'; ... }; % Call generalized class creation function (built-in) create_user_class(userClass, deriveFromClass, addProperties);
Creating a Class Instantiation Function
Simulink uses the class instantiation function to create an instance of a class. It finds the class instantiation function by looking in the class directory for an M-file that has the same name as the class. For example, if the name of the class is Parameter
, Simulink looks for an M-file named Parameter.m
and containing a function named Parameter
that returns a handle to the function. A minimal instantiation function takes no arguments and simply invokes the default instantiation function for the class as illustrated in the following example.
function h = Parameter() % Class instantiation function. % Instantiate class h = UserDefined.Parameter;
An instantiation function can optionally take a variable number of arguments. The function can use the optional arguments to initialize the properties of the object as illustrated in the following example.
function h = Parameter(varargin) % Class instantiation function. % Instantiate class h = UserDefined.Parameter; % Initialize property values (optional) if nargin == 1 % If only one argument provided, treat it as the "Value". h.Value = varargin{1}; end
Creating Data Object Properties
A data object class inherits the properties of its parent class. You can define additional properties for the class in its constructor. To do so, pass an n-by-3 cell array to the class constructor function (see create_user_class) where n
is the number of properties to be specified. Each row of the array should specify the name (e.g., 'angle'
), type (e.g., 'double'
), and default value of the corresponding property.
The Simulink.Signal
and Simulink.Parameter
classes are likely to acquire new properties in future releases. Consequently, when deriving classes from these classes, you should use property names that are not likely to conflict with names of future properties of these classes. One approach to avoid a naming conflict is to append your company's name to names of properties of derived classes.
Data Object Functions
Simulink provides the following functions for creating and manipulating Simulink data objects and classes.
create_user_class. Use this function in a class constructor file (schema.m
) to create a new data object class. It takes three arguments
'UserDefined.Parameter'
)'Simulink.Parameter'
)create_user_enumtype. Use this function in a class constructor to create an enumerated data type, that is, a data type with a specified set of valid values. You can then use the enumerated type as the type of one or more of a class's properties. The create_user_enumtype
function takes two arguments.
For example, the following code creates an enumerated type named colors
.
create_user_enumtype('colors', {'red', 'green', 'blue'});
findpackage. Returns a handle to a package object, for example,
h_SimulinkPackage = findpackage('Simulink');
findclass. Returns a handle to a class, for example,
h_SimulinkParameter = findclass(h_SimulinkPackage, 'Parameter');
findproperty. Returns a handle to an object property, for example,
h_ParamValue = findparameter(h_SimulinkParameter, 'Value');
![]() | Working with Data Objects | The Simulink Data Explorer | ![]() |