Skip to Main Content Skip to Search
Product Documentation

Calling Object Methods

What Operations Can You Perform

Methods define all aspects of an object's behavior. Consequently, most classes implement many methods that an object user is unlikely to call directly. The user documentation for the object you are using describes the operations you can perform on any particular object.

You can list the methods defined by a class with the methods or methodsview functions:

methods('timeseries')

Methods for class timeseries:

addevent              gettsbetweenevents    set                   
addsample             horzcat               setabstime            
createTstoolNode      idealfilter           setinterpmethod       
ctranspose            init                  setprop               
...

Method Syntax

You call an object's method using dot notation:

returnedValue = object.MethodName(args,...)

You also can call a method using function syntax, passing the object as the first (left-most) argument.

For example, MException objects have a getReport method that returns information about the error.

try
   surf
catch me
   disp(me.getReport) 
end

Error using ==> surf at 50
Not enough input arguments.

Dot and function notation are usually equivalent. That is, both of the following statements return the MException report:

rpt = getReport(me); % Call getReport using function notation
rpt = me.getReport;  % Call getReport using dot notation

Calling the Correct Method

It is possible for the function syntax to call an unexpected method if there is more than one object in the argument list. Suppose there are two classes, ClassA and ClassB, that define a method called addData. Suppose further that ClassA is defined as being inferior to ClassB in precedence (something that the class designer can do in the class definition). In this situation, given objA is of ClassA and objB is of ClassB, the following two statement call different methods:

addData(objA,objB) % Calls objB.addData
objA.addData(objB) % Calls objA.addData

If ClassA and ClassB had equal precedence, then the left-most argument determines which method MATLAB calls (i.e., objA.addData in both statements).

It is unlikely that you will encounter this particular scenario, however, if you are calling a method that accepts more than one object as arguments, using dot notation removes any ambiguity about which object's method MATLAB calls.

Class of Objects Returned by Methods

While methods sometimes return objects of the same class, this is not always the case. For example, the MException object's getReport returns a character string:

try
   surf
catch me
   rpt = me.getReport;
end

whos
  Name      Size             Bytes  Class         Attributes

  me        1x1               1118  MException              
  rpt       1x126              252  char

Methods can return any type of value and properties can contain any type of value. However, class constructor methods always return an object or array of objects of the same type as the class.

  


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