MATLAB Object-Oriented Programming

(Joyce) #1

Redefine Indexed Reference


The DocPolynom class redefines indexed reference to support the use of objects
representing polynomials. In the DocPolynom class, a subscripted reference to an object
causes an evaluation of the polynomial with the value of the independent variable equal to
the subscript.


For example, given the following polynomial:


Create a DocPolynom object p:


p = DocPolynom([1 0 -2 -5])


p =
x^3 - 2*x - 5


The following subscripted expression evaluates the value of the polynomial at x = 3 and
at x = 4, and returns the resulting values:


p([3 4])


ans =
16 51


Indexed Reference Design Objectives


Redefine the default subscripted reference behavior by implementing a subsref method.


If a class defines a subsref method, MATLAB calls this method for objects of this class
whenever a subscripted reference occurs. The subsref method must define all the
indexed reference behaviors, not just a specific case that you want to change.


The DocPolynom subsref method implements the following behaviors:



  • p(x = [a 1 ...an]) — Evaluate polynomial at x = a.

  • p.coef — Access coef property value

  • p.disp — Display the polynomial as a MATLAB expression without assigning an
    output.

  • obj = p.method(args) — Use dot notation to call methods arguments and return a
    modified object.


Representing Polynomials with Classes
Free download pdf