function this = subsasgn(this, index, varargin)
% A Comprehensive Guide to Object Oriented Programming in MATLAB
% Chapter 9 example subsasgn
% (c) 2004 Andy Register

% Generic method

switch index(1).type

case '.'
try
this = set(this, index, varargin{end:-1:1});
catch
rethrow(lasterror);
end

case '()'
if isempty(this)
% due to superiorto, need to look at this and varargin
if isa(this, mfilename('class'))
this = eval(class(this));
else
this = eval(class(varargin{1}));
end
end
if length(index) == 1
this = builtin('subsasgn', this, index, varargin{end:-1:1});
else
this_subset = this(index(1).subs{:}); % get the subset
this_subset = subsasgn(this_subset, index(2:end), varargin{:});
this(index(1).subs{:}) = this_subset; % put subset back
end

case '{}'
error(['??? ' class(this) ' object, is not a cell array']);

otherwise
error(['??? Unexpected index.type of ' index(1).type]);
end