Products & Services Industries Academia Support User Community Company

Learn more about MATLAB   

Getting Information About Objects

The Class of Workspace Variables

Knowing the class of the variables you are working with enables you to use them most effectively. For example, consider the following variable created in your workspace:

load count.dat % Load some data
tsobj = timeseries(count(:,1),1:24,'Name','Data1');
>> whos
  Name        Size            Bytes  Class   
  count      24x3               576  double  
  tsobj      24x1               261  timeseries

The whos command lists information about your workspace variables. Notice that the variable loaded from the count.dat file (count) is an array of doubles. You know, therefore, that you can perform indexing and arithmetic operations on this array. For example:

newcount = sum(count,2);
newcount(8:15) = NaN;
bar(newcount)

Indexed assignment and the bar function work with inputs of class double.

However, the timeseries class does not define a bar method for timeseries objects. The timeseries class defines a plot method for graphing because the class design specified a line plot as the best way to represent time series data.

Extracting Data From Object Properties

Suppose you have a timeseries object and you want to work directly with the numeric values of the timeseries data. You can extract data from the object properties and assign these values to an array. For example

load count
tsobj = timeseries(sum(count,2),1:24,'Name','DataSum');
d = tsobj.Data;
t = tsobj.Time;
n = tsojb.Name;
d(8:15) = NaN;
bar(t,d); title(n)

Testing for the Class of an Object

Suppose you create a function that operates on more than one class of object. If you have a timeseries object, you call the timeseries plot method, but if the object is of class double, you can call the bar function (which isn't supported by timeseries objects). You could use isa as in the following code to make this determination:

if isa(obj,'timeseries')
   plot(obj)
elseif isa(obj,'double')
   bar(obj)
end

Information About Class Members

These functions provide information about the object.

FunctionPurpose
class

Return class of object

events

List of event names defined by the class

methods

List of methods implemented by the class

methodsview

Information on class methods in separate window

properties

List of class property names

Logical Tests for Objects

In functions, you might need conditional statements to determine the status of an object before performing certain actions. For example, you might perform different actions based on the class of an object (see Testing for the Class of an Object). The following functions provide logical tests for objects:

FunctionPurpose
isa

Determine whether argument belongs to a particular class. True for object's class and all of object's superclasses.

isequal

Determine if two objects are equal.

isobject

Determine whether the input is a MATLAB object.

Testing for Object Equality

isequal finds two objects to be equal if all the following conditions are met:

isequal tests the value of every array element in every property and every property of every object contained in the objects being tested. As contained objects are tested for equality, MATLAB calls each object's own version of isequal (if such versions exist).

If objects contain large amounts of data stored in other objects, then testing for equality can be a time-consuming process.

Identifying MATLAB Objects

The isobject function returns true only for MATLAB objects. For Sun Java objects, use isjava. For Handle Graphics objects, use ishandle.

Displaying Objects

When you issue commands that return objects and do not terminate those commands with a semicolon, or when you pass an object to the disp function, MATLAB displays information about the object. For example:

hobj = containers.Map({'Red Sox','Yankees'}, {'Boston','New York'})
hobj = 
  containers.Map handle
  Package: containers
  Properties:
        Count: 2
      KeyType: 'char'
    ValueType: 'char'
  Methods, Events, Superclasses

This information includes links (shown in blue) to documentation on the object's class and superclasses, and lists of methods, events, and superclasses. Properties and their current values are also listed.

Some classes (timeseries, for example) redefine how they display objects to provide more useful information for this particular class.

Getting Help for MATLAB Objects

You can get documentation for MATLAB objects using the doc command with the class name. To see the reference pages for the objects used in this chapter, use the following commands:

doc timeseries
doc MException
doc containers.Map % Include the package name
  


Recommended Products

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

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