Using Simulink    

Working with Data Objects

Simulink data objects allow you to specify information about the data used in a Simulink model (i.e., signals and parameters) and to store the information with the data itself in the model. Simulink uses properties of data objects to determine the tunability of parameters and the visibility of signals and to generate code. You can use data objects to specify information important to correct simulation of the model, such as minimum and maximum values for parameters. Further, you can store data objects with the model. Simulink thus allows you to create self-contained models.

Data Object Classes

A data object is an instance of another object called a data object class. A data object class defines the properties of the data objects that are its instances and methods for creating and manipulating the instances. Simulink comes with two built-in data classes, Simulink.Parameter and Simulink.Signal, that define parameter and signal data objects, respectively.

Data Object Properties

A property of a data object specifies an attribute of the data item that the object describes, such as the value or storage type of the data item. Every property has a name and a value. The value can be an array or a structure, depending on the property.

Data Object Packages

Simulink organizes classes into groups of classes called packages. Simulink comes with a single package named Simulink. The Simulink classes, Simulink.Parameter and Simulink.Signal, belong to the Simulink package. You can create additional packages and define classes that belong to those classes.

Qualified Names

When referring to a class on the MATLAB command line or in an M-file program, you must specify both the name of the class and the name of the class's package, using the following dot notation:

The PackageName.ClassName notation is called the qualified name of the class. For example, the qualified name of the Simulink parameter class is Simulink.Parameter.

Two packages can have identically named but distinct classes. For example, packages A and B can both have a class named C. You can refer to these classes unambiguously on the MATLAB command line or in an M-file program, using the qualified class name for each. Packages enable you to avoid naming conflicts when creating classes. For example, you can create your own Parameter and Signal classes without fear of conflicting with the similarly named Simulink classes.

Creating Data Objects

You can use either the Simulink Data Explorer or MATLAB commands to create Simulink data objects. See The Simulink Data Explorer for information on using the Data Explorer to create data objects.

Use the following syntax to create a data object at the MATLAB command line or in a program:

where h is a MATLAB variable, package is the name of the package to which the class belongs, class is the name of the class, and arg1, arg2, ... argn, are optional arguments passed to the object constructor. (Constructors for the Simulink.Parameter and Simulink.Signal classes do not take arguments.) For example, to create an instance of the Simulink.Parameter class, enter

at the MATLAB command line.

This command creates an instance of Simulink.Parameter and stores its handle in hGain.

Accessing Data Object Properties

You can use either the Simulink Data Explorer (see The Simulink Data Explorer) or MATLAB commands to get and set a data object's properties. See The Simulink Data Explorer for information on how to use the Data Explorer to display and set object properties.

To access a data object's properties at the MATLAB command line or in an M-file program, use the following notation:

where hObject is the handle to the object and property is the name of the property. For example, the following code

creates a Simulink block parameter object and sets the value of its Value property to 5. You can use dot notation recursively to access the fields of structure properties. For example, gain.RTWInfo.StorageClass returns the StorageClass property of the gain parameter.

Invoking Data Object Methods

Use the syntax

or

to invoke a data object method, where hObject is the object's handle. Simulink defines the following methods for data objects.

Saving and Loading Data Objects

You can use the MATLAB save command to save data objects in a MAT-file and the MATLAB load command to restore them to the MATLAB workspace in the same or a later session. Definitions of the classes of saved objects must exist on the MATLAB path for them to be restored. If the class of a saved object acquires new properties after the object is saved, Simulink adds the new properties to the restored version of the object. If the class loses properties after the object is saved, Simulink restores only the properties that remain.

Using Data Objects in Simulink Models

You can use data objects in Simulink models as parameters and signals. Using data objects as parameters and signals allows you to specify simulation and code generation options on an object-by-object basis.

Using Data Objects as Parameters

You can use an instance of Simulink.Parameter class or a descendant class as a block parameter. To use a parameter object as a block parameter:

  1. Create the parameter object at the MATLAB command line or in the Simulink Data Explorer.
  2. Set the value of the object's Value property to the value you want to specify for the block parameter.
  3. Set the parameter object's storage class and type properties to select tunability (see The Simulink Data Explorer) and/or code generation options (see the Real-Time Workshop documentation for more information).
  4. Specify the parameter object as the block parameter in the block's parameter dialog box or in a set_param command.

See Creating Persistent Parameter and Signal Objects for information on how to create parameter objects that persist across a session.

Using Parameter Objects to Specify Parameter Tunability

If you want the parameter to be tunable even when the Inline parameters simulation option is set (see Model parameter configuration), set the parameter object's RTWInfo.StorageClass property to any value but 'Auto' (the default).

gain.RTWInfo.StorageClass = 'SimulinkGlobal';

If you set the RTWInfo.StorageClass property to any value other than Auto, you should not include the parameter in the model's tunable parameters table (see Model Parameter Configuration Dialog Box).

Using Data Objects as Signals

You can use an instance of Simulink.Signal class or a descendent class to specify signal properties. To use a data object as a signal object to specify signal properties,

  1. Create the signal data object in the model workspace.
  2. Set the storage class and type properties of the signal object to specify the visibility of the signal (see Using Signal Objects to Specify Test Points) and code generation options (see the Real-Time Workshop documentation for information on using signal properties to specify code generation options).
  3. Change the label of any signal that you want to have the same properties as the signal data object to have the same name as the signal.

See Creating Persistent Parameter and Signal Objects for information on creating signal objects that persist across Simulink sessions.

Using Signal Objects to Specify Test Points

If you want a signal to be a test point (i.e., always available for display on a floating scope during simulation), set the RTWInfo.StorageClass property of the corresponding signal object to any value but auto.

Creating Persistent Parameter and Signal Objects

To create parameter and signal objects that persist across Simulink sessions, first write a script that creates the objects or create the objects with the Simulink Data Explorer (see The Simulink Data Explorer) or at the command line and save them in a MAT-file (see Saving and Loading Data Objects). Then use either the script or a load command as the PreLoadFcn callback routine for the model that uses the objects. For example, suppose you save the data objects in a file named data_objects.mat and the model to which they apply is open and active. Then, entering the following command

at the MATLAB command line sets load data_objects as the model's preload function. This in turn causes the data objects to be loaded into the model workspace whenever you open the model.


  Typecasting Parameters Creating Data Object Classes