From: Matt J on

Here's another thread where this was discussed,

http://www.mathworks.com/matlabcentral/newsreader/view_thread/252986#654962

and below is a slightly upgraded version of the @FancyNumeric class that I proposed there. It lets you do things like the following



>> x=FancyNumeric([3 4])

x =

3 4

>> x.p=1; %same as x+=1

x =

4 5

>> x.m=2 %same as x-=2

x =

2 3

>> x.t=3 %same as x*=3

x =

6 9




%%%%%%%%%%%%%%%%%%%
classdef FancyNumeric<double

methods

function obj=FancyNumeric(data)
obj=obj(a)double(data);
end

function obj=subsref(obj,IndexStruct)

obj=FancyNumeric(builtin('subsref',obj.data,IndexStruct));

end


function obj=subsasgn(obj,IndexStruct,RHS)

x=double(obj);




switch IndexStruct.type

case '.'

switch IndexStruct.subs

case 'p'

x=x+RHS;

case 'm'

x=x-RHS;

case 't'

x=x.*RHS;

case 'd'

x=x./RHS;

otherwise

error 'Unsupported operation'

end%subs

otherwise%numeric assignment

x=builtin('subsasgn',x,IndexStruct,RHS);



end%type

obj=FancyNumeric(x);
end


function display(obj)

l=inputname(1);
T=evalc('double(obj)'); %only way to get at the builtin display method
if ~isempty(l),
T=strrep(T,'ans =',[l ' =']);
end

jj=find(T~=sprintf('\n'),1,'last');

T=T(1:jj);

disp(T), disp ' '

end


end

end