Using Simulink | ![]() ![]() |
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.
Note You can use the Data Class Designer to create custom storage classes. See the RTW documentation for information on custom storage classes. |
Creating a Data Object Class
To create a class with the Data Class Designer:
matlabroot/toolbox/simulink
. See Creating a Class Package for information on creating your own class packages.
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:
Simulink grays the inherited properties to indicate that they cannot be redefined by the child class.
Defining Class Properties
Note
The property name must be unique to the class. Unlike class names, property names are not case sensitive. For example, Simulink treats Value and value as referring to the same property.
|
The following rules apply to entering factory values for properties:
[0 1; 1 0]
. Simulink evaluates the expression that you enter to check its validity. Simulink displays a warning message if evaluating the expression results in an error. Regardless of whether an evaluation error occurs, Simulink stores the expression as the factory value of the property. This is because an expression that is invalid at define time might be valid at run-time.
double
or int32
property. Simulink evaluates the expression and stores the result as the property's factory value.
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:
Color
.
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.
switch nargin case 0 % No input arguments - no action case 1 % One input argument h.ParamName = varargin{1}; otherwise warning('Invalid number of input arguments'); end
Creating a Class Package
To create a new package to contain your classes:
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 | ![]() |