23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

9 Classes and Objects<br />

to the .c field of the object’s structure. The class function creates the<br />

polynom object, which is then returned by the constructor.<br />

An example use of the polynom constructor is the statement<br />

p = polynom([1 0 -2 -5])<br />

This creates a polynomial with the specified coefficients.<br />

Converter Methods for the Polynom Class<br />

A converter method converts an object of one class to an object of another<br />

class. Two of the most important converter methods contained in <strong>MATLAB</strong><br />

classes are double and char. Conversiontodouble produces the <strong>MATLAB</strong><br />

traditional matrix, although this may not be appropriate for some classes.<br />

Conversion to char is useful for producing printed output.<br />

The Polynom to Double Converter<br />

The double converter method for the polynom class is a very simple M-file,<br />

@polynom/double.m, which merely retrieves the coefficient vector.<br />

function c = double(p)<br />

% POLYNOM/DOUBLE Convert polynom object to coefficient vector.<br />

% c = DOUBLE(p) converts a polynomial object to the vector c<br />

% containing the coefficients of descending powers of x.<br />

c = p.c;<br />

On the object p,<br />

p = polynom([1 0 -2 -5])<br />

the statement<br />

double(p)<br />

returns<br />

ans =<br />

1 0 -2 -5<br />

9-28

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!