MATLAB Object-Oriented Programming

(Joyce) #1
Example Code Discussion
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;
if d > 0
s(ind) = {'*'};
ind = ind + 1;
end
end
end
if d >= 2
s(ind) = {['x^' int2str(d)]};
ind = ind + 1;
elseif d == 1
s(ind) = {'x'};
ind = ind + 1;
end
end

Convert DocPolynom object to
char that represents the
expression:

y = f(x)

See “Convert DocPolynom
Objects to Other Types” on page
19-15

19 Defining Custom Data Types

Free download pdf