Using Simulink    

Creating Data Object Classes

The Simulink Data Class Designer allows you to define your own data object classes. To define a class with the Data Class Designer, you enter the package, name, parent class, properties, and other characteristics of the class in a dialog box. The Data Class Designer then generates P-code that defines the class. You can also use the Data Class Designer to change the definitions of classes that it created, for example, to add or remove properties.

Creating a Data Object Class

To create a class with the Data Class Designer:

  1. Select Data class designer from the Simulink Tools menu.
  1. The Data Class Designer's dialog box appears.

  1. Select the name of the package in which you want to create the class from the Package name list.
  1. Do not create a class in any of Simulink's built-in packages, i.e., packages in matlabroot/toolbox/simulink. See Creating a Class Package for information on creating your own class packages.

  1. Click the New button on the Classes pane of the Data Class Designer dialog box.
  2. Enter the name of the new class in the Class name field on the Classes pane.

  1. Press Enter or click the OK button on the Classes pane to create the specified class in memory.
  2. Select a parent class for the new class (see Specifying a Parent for a Class).
  3. Define the properties of the new class (see Defining Class Properties).
  4. If necessary, create initialization code for the new class (see Creating Initialization Code).
  5. Click Confirm changes.
  1. Simulink displays the Confirm changes pane.

  1. Click Write all or select the package containing the new class definition and click Write selected to save the new class definition.

You can also use the Classes pane to perform the following operations.

Copy a class.   To copy a class, select the class in the Classes pane and click Copy. Simulink creates a copy of the class under a slightly different name. Edit the name, if desired, click Confirm changes, and click Write all or, after selecting the appropriate package, Write selected to save the new class.

Rename a class.   To rename a class, select the class in the Classes pane and click Rename. The Class name field becomes editable. Edit the field to reflect the new name. Save the package containing the renamed class, using the Confirm changes pane.

Remove a class from a package.   To remove a class definition from the currently selected package, select the class in the Classes pane and click Remove. Simulink removes the class from the in-memory definition of the class. Save the package that formerly contained the class.

Specifying a Parent for a Class

To specify a parent for a class:

  1. Select the name of the class from the Class name field on the Classes pane.
  2. Select the package name of the parent class from the lefthand Derived from list box.

  3. Select the parent class from the righthand Derived from list.

  1. Save the package containing the class.

Defining Class Properties

To add a property to a class,

  1. Select the name of the class from the Class name field on the Classes pane.
  2. Select the New button next to the Properties of this class field on the Classes pane.
  1. Simulink creates a property with a default name and value and displays the property in the Properties of this class field.

  1. Enter a name for the new property in the Property Name column.

  1. Select the data type of the property from the Property Type list.
  1. The list includes built-in property types and any enumerated property types that you have defined (see Defining Enumerated Property Types).

  1. If you want the property to have a default value, enter the default value in the Factory Value column.
  1. The default value is the value the property has when an instance of the associated class is created. The initialization code for the class can override this value (see Creating Initialization Code for more information).

    The following rules apply to entering factory values for properties:

  1. Save the package containing the class with new or changed properties.

Defining Enumerated Property Types

An enumerated property type is a property type whose value must be one of a specified set of values, for example, red, blue, or green. An enumerated property type is valid only in the package that defines it.

To create an enumerated property type:

  1. Select the Enumerated Property Types pane of the Data Class Designer.

  2. Click the New button next to the Property type name field.
  1. Simulink creates an enumerated type with a default name.

  1. Change the default name in the Property type name field to the desired name for the property.
  1. The currently selected package defines an enumerated property type and the type can be referenced only in the package that defines it. However, the name of the enumerated property type must be globally unique. There cannot be any other built-in or user-defined enumerated property with the same name. If you enter the name of an existing built-in or user-defined enumerated property for the new property, Simulink displays an error message.

  1. Click the OK button.
  1. Simulink creates the new property in memory and enables the Enumerated strings field on the Enumerated Property Types pane.

  1. Enter the permissible values for the new property type Enumerated strings field, one per line.
  1. For example, the following Enumerated strings field shows the permissible values for an enumerated property type named Color.

  1. Click Apply to save the changes in memory.
  2. Click Confirm changes. Then click Write all to save this change.

You can also use the Enumerated Property Type pane to copy, rename, and remove enumerated property types.

Don't forget to save the package containing the modified enumerated property type.

Creating Initialization Code

You can specify code to be executed when Simulink creates an instance of a data object class. To specify initialization code for a class, select the class from the Class name field of the Data Class Designer and enter the initialization code in the Class initialization field.

The Data Class Designer inserts the code that you enter in the Class initialization field in the class instantiation function of the corresponding class. Simulink invokes this function when it creates an instance of this class. The class instantiation function has the form

where h is the handle to the object that is created and varargin is a cell array that contains the function's input arguments.

By entering the appropriate code in the Data Class Designer, you can cause the instantiation function to perform such initialization operations as

For example, suppose you want to let a user initialize the ParamName property of instances of a class named MyPackage.Parameter. The user does this by passing the initial value of the ParamName property to the class constructor.

The following code in the instantiation function would perform the required initialization.

Creating a Class Package

To create a new package to contain your classes:

  1. Click the New button next to the Package name field of the Data Class Designer.

  1. Edit the Package name field to contain the package name that you want.

  2. Click OK to create the new package in memory.
  3. In the package Parent directory field, enter the path of the directory where you want Simulink to create the new package.

  1. Simulink creates the specified directory, if it does not already exist, when you save the package to your file system in the succeeding steps.

  1. Click the Confirm changes button on the Data Class Designer.
  1. Simulink displays the Packages to write panel.

  1. To enable use of this package in the current and future sessions, ensure that the Add parent directory to MATLAB path box is selected (the default).
  1. This adds the path of the new package's parent directory to the MATLAB path.

  1. Click Write all or select the new package and click Write selected to save the new package.

You can also use the Data Class Designer to copy, rename, and remove packages.

Copying a package.   To copy a package, select the package and click the Copy button next to the Package name field. Simulink creates a copy of the package under a slightly different name. Edit the new name, if desired, and click OK to create the package in memory. Then save the package to make it permanent.

Renaming a package.   To rename a package, select the package and click the Rename button next to the Package name field. The field becomes editable. Edit the field to reflect the new name. Save the renamed package.

Removing a package.   To remove a package, select the package and click the Remove button next to the Package name field to remove the package from memory. Click the Confirm changes button to display the Packages to remove panel. Select the package and click Remove selected to remove the package from your file system or click Remove all to remove all packages that you have removed from memory from your file system as well.


  Working with Data Objects The Simulink Data Explorer