MATLAB Object-Oriented Programming

(Joyce) #1
e = @(x)eval(p);
y = zeros(length(x));
disp(['y = ',p])
for k = 1:length(x)
y(k) = e(x(k));
disp([' ',num2str(y(k)),...
' = f(x = ',...
num2str(x(k)),')'])
end
end

function b = subsref(a,s)
% SUBSREF Implementing the following syntax:
% obj([1 ...])
% obj.coef
% obj.disp
% out = obj.method(args)
% out = obj.method
switch s(1).type
case '()'
ind = s.subs{:};
b = polyval(a.coef,ind);
case '.'
switch s(1).subs
case 'coef'
b = a.coef;
case 'disp'
disp(a)
otherwise
if length(s)>1
b = a.(s(1).subs)(s(2).subs{:});
else
b = a.(s.subs);
end
end
otherwise
error('Specify value for x as obj(x)')
end
end % subsref

function r = plus(obj1,obj2)
% PLUS Implement obj1 + obj2 for DocPolynom
obj1 = DocPolynom(obj1);
obj2 = DocPolynom(obj2);

19 Defining Custom Data Types

Free download pdf