MATLAB Object-Oriented Programming

(Joyce) #1

end as Object Index


In this section...
“Define end Indexing for an Object” on page 17-35
“The end Method” on page 17-36

Define end Indexing for an Object


When you use end in an object indexing expression, such as A(4:end), the end function
returns the index value corresponding to the last element in that dimension.

Classes can overload the end function to implement specialized behavior. If your class
defines an end method, MATLAB calls that method to determine how to interpret the
expression.

The end method has the calling syntax:

ind = end(A,k,n)

The arguments are described as follows:


  • A is the object

  • k is the index in the expression using the end syntax

  • n is the total number of indices in the expression

  • ind is the index value to use in the expression


For example, consider the 3-by-5 array A. When MATLAB encounters the expression:

A(end-1,:)

MATLAB calls the end method defined for the object A using the arguments:

ind = end(A,1,2)

These arguments mean that the end statement occurs in the first index and there are two
indices. The end class method returns the index value for the last element of the first
dimension (from which 1 is subtracted in this case). The original expression is evaluated
as:

A(3-1,:)

end as Object Index
Free download pdf