Skip to Main Content Skip to Search
Product Documentation

Key Object Concepts

Basic Concepts

There are some basic concepts that are fundamental to objects. Objects represent something in the real world, like an error condition or the set of data you collected in a product test. Objects enable you to do something useful, like provide an error report or analyze and present the results of tests.

There are basic components that MATLAB uses to realize the design of an object. These components include:

Classes Describe How to Create Objects

A class defines a set of similar objects. It is a description from which MATLAB creates a particular instance of the class, and it is the instance (that is, the object) that contains actual data. Therefore, while there is a timeseries class, you work with timeseries objects.

Classes are defined in code files — either as separate .m files or built-in to the MATLAB executable. Objects are specific representations of a class that you access through workspace variables.

Properties Contain Data

Objects store data in properties. Consider a timeseries object as an example. timeseries object properties contain time series data, corresponding time values, and related information, such as units, events, data quality, and interpolation method. MATLAB objects enable you to access property data directly (see Accessing Object Data for information on property syntax).

Properties are sometimes called fields in other programming languages and are similar to the fields of MATLAB structures. Properties have descriptive names, such as Data and DataInfo, in the case of timeseries objects, and can contain any kind of MATLAB data, including other objects.

An object, then, is a container for a predefined set of data. Unlike a cell array or structure, you cannot add new properties or delete defined properties from an object. Doing so would compromise the object's intended purpose and violate the class design.

The class design can restrict the values you can assign to a property. For example, a Length property might restrict possible values to positive integers or might be read only and determine its own value when queried.

Methods Implement Operations

Class methods are functions designed to work with objects of a particular class. Methods enable the class designer to implement specific operations that are optimized for the data contained in the object. You do not have to extract the data from the object, modify its format, and pass it to a general-purpose MATLAB function because the class defines methods with an awareness of the object's structure.

Methods can define operations that are unique to a particular class of object, such as adding a data sample to an existing set of time series data, or can overload common operations in a way that makes sense for the particular object. For example, timeseries objects have an addsample method to add a new data sample to an existing timeseries object. Also, timeseries overloads the MATLAB plot function to work with timeseries objects.

MATLAB software determines which overloaded version of a method to call based on the class of the object passed as an argument. If you execute a MATLAB statement like:

tsobjnew = tsobj1 + tsobj2;

where tsobj1 and tsobj2 are timeseries objects, MATLAB calls the timeseries version of the + operation (if defined) and returns a new timeseries object.

Because the timeseries class defines the operation, you can add a timeseries object to a scalar number:

tsobjnew = tsobj1 + 4;

The class definition determines what happens when you add a scalar double to a timeseries object (the scalar is added to each Data value).

Methods make working with objects convenient for the user, but also provide advantages to the class designer. Methods hide implementation details from users—you do not need to create your own functions to access and manipulate data, as you would when using general-purpose data structures like structs and cell arrays. This provides the flexibility to change the internal design of an object without affecting object clients (i.e., application programs that use the objects).

Events are Notices Broadcast to Listening Objects

Classes can defined names for specific actions and trigger the broadcast of notices when those actions occur. Listeners respond to the broadcast of an event notice by executing a predefined function.

For example, objects can listen for the change of the value of a property and execute a function when that change occurs. If an object defines an event for which you can define a listening object, the object's documentation describes that event. See Events — Sending and Responding to Messages for information on how class designers use events.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS