MATLAB Object-Oriented Programming

(Joyce) #1
if nargin > 0
if isa(c,'DocPolynom')
obj.coef = c.coef;
else
obj.coef = c(:).';
end
end
end % DocPolynom
function obj = set.coef(obj,val)
if ~isa(val,'double')
error('Coefficients must be doubles')
end
% Remove leading zeros
ind = find(val(:).'~=0);
if ~isempty(ind);
obj.coef = val(ind(1):end);
else
obj.coef = val;
end
end % set.coef

function c = double(obj)
c = obj.coef;
end % double

function str = char(obj)
% Created a formated display of the polynom
% as powers of x
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) = {' - '};

19 Defining Custom Data Types

Free download pdf