MATLAB Object-Oriented Programming

(Joyce) #1
class(c)
ans =
double

The Character Converter

The char method produces a char vector that represents the polynomial displayed as
powers of x. The char vector returned is a syntactically correct MATLAB expression.

The char method uses a cell array to collect the char vector components that make up
the displayed polynomial.

The disp method uses the char method to format the DocPolynom object for display.
The evalPoly method uses char to create the MATLAB expression to evaluate.

Users of DocPolynom objects are not likely to call the char or disp methods directly, but
these methods enable the DocPolynom class to behave like other data classes in
MATLAB.

Here is the char method.
methods
function str = char(obj)
if all(obj.coef == 0)
s = '0';
str = s;
return
else
d = length(obj.coef)-1;
s = cell(1,d);
ind = 1;
for a = obj.coef;
if a ~= 0;
if ind ~= 1
if a > 0
s(ind) = {' + '};
ind = ind + 1;
else
s(ind) = {' - '};
a = -a;
ind = ind + 1;
end
end
if a ~= 1 || d == 0
if a == -1
s(ind) = {'-'};
ind = ind + 1;
else
s(ind) = {num2str(a)};
ind = ind + 1;

19 Defining Custom Data Types

Free download pdf