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.

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.

where PackageName is the name of the new package.

Creating a Class

To create a data object class,

  1. Create a directory named @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.
  2. Create a class constructor in the class directory.
  3. Create a class instantiation function in the class directory.

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.

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.

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.

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

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.

findpackage.   Returns a handle to a package object, for example,

findclass.   Returns a handle to a class, for example,

findproperty.   Returns a handle to an object property, for example,


 Working with Data Objects The Simulink Data Explorer