MATLAB Object-Oriented Programming

(Joyce) #1

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


subsref Implementation Details

The subsref method overloads the subsref function.

For example, consider a call to the polyval function:

p = DocPolynom([1 0 -2 -5])
p =
x^3 - 2*x - 5
polyval(p.coef,[3 5 7])
ans =
16 110 324

The polyval function requires the:


  • Polynomial coefficients

  • Values of the independent variable at which to evaluate the polynomial


The polyval function returns the value of f(x) at these values. subsref calls polyval
through the statements:

case '()'
ind = s.subs{:};
b = polyval(a.coef,ind);

When implementing subsref to support method calling with arguments using dot
notation, both the type and subs structure fields contain multiple elements.

The subsref method implements all subscripted reference explicitly, as show in the
following code listing.

methods
function b = subsref(a,s)
switch s(1).type
case '()'
ind = s.subs{:};
b = polyval(a.coef,ind);
case '.'
switch s(1).subs
case 'coef'
b = a.coef;

19 Defining Custom Data Types

Free download pdf