Programming |
Object Indexing Within Methods
If a subscripted reference is made within a class method, MATLAB uses its built-in subsref
function to access data within the method's own class. If the method accesses data from another class, MATLAB calls the overloaded subsref
function in that class. The same holds true for subscripted assignment and subsasgn
.
The following example shows a method, testref
, that is defined in the class, employee
. This method makes a reference to a field, address
, in an object of its own class. For this, MATLAB uses the built-in subsref
function. It also references the same field in another class, this time using the overloaded subsref
of that class.
% ---- EMPLOYEE class method: testref.m ---- function testref(myclass,otherclass) myclass.address % use built-in subsref otherclass.address % use overloaded subsref
The example creates an employee
object and a company
object.
The employee
class method, testref
, is called. MATLAB uses an overloaded subsref
only to access data outside of the method's own class.
testref(empl,comp) ans = % built-in subsref was called Chicago ans = % @company\subsref was called Executing @company\subsref ... Natick
Handling Subscripted Assignment | Defining end Indexing for an Object |
© 1994-2005 The MathWorks, Inc.