function a = subsasgn(a,Struct,rhs)
%SUBSASGN Overloaded method for A(i)=B, A{I}=B, and A.field=B.
%       S = SUBSASGN(A,S,B) is called for the syntax A(I)=B, A{I}=B, or
%       A.I=B when A is an object.  S is a structure array with the fields:
%           type -- string containing '()', '{}', or '.' specifying the
%                   subscript type.
%           subs -- Cell array or string containing the actual subscripts.
%
%       For instance, the syntax A(1:2,:)=B calls A=SUBSASGN(A,S,B) where
%       S is a 1-by-1 structure with S.type='()' and S.subs = {1:2,':'}. A
%       colon used as a subscript is passed as the string ':'.
%
%       Similarly, the syntax A{1:2}=B calls A=SUBSASGN(A,S,B) where
%       S.type='{}' and the syntax A.field=B calls SUBSASGN(A,S,B) where
%       S.type='.' and S.subs='field'.
%
%       These simple calls are combined in a straightforward way for
%       more complicated subscripting expressions.  In such cases
%       length(S) is the number of subscripting levels.  For instance, 
%       A(1,2).name(3:5)=B calls A=SUBSASGN(A,S,B) where S is 3-by-1
%       structure array with the following values:
%           S(1).type='()'       S(2).type='.'        S(3).type='()'
%           S(1).subs={1,2}      S(2).subs='name'     S(3).subs={3:5}
%
%       See also SUBSREF.

%       Copyright (c) 1986-97 by The MathWorks, Inc.
%       $Revision: 1.3 $
%       Built-in function.

ni = nargin;
if ni==1,
   return
elseif ni~=3,
   error('Wrong number of inputs.')
end

StructL = length(Struct);
switch StructL
case 0
   error('Zero length structure.')
case 1
   switch Struct.type
   case '.'
      eval(['a.' Struct.subs '=rhs;']);
   case '()'
      a(Struct.subs{:}) = rhs;
   case '{}'
      a{Struct.subs{:}} = rhs;
   otherwise
      error('Invalid type field.')
   end
otherwise
   switch Struct(1).type
   case '.'
      rhs = subsasg2(eval(['a.' Struct(1).subs]),Struct(2:end),rhs);
      eval(['a.' Struct(1).subs '=rhs;']);
   case '()'
      % Do not allow ()()
      if strcmp(Struct(2).type,'()'),
         error('Inconsistently placed () in subscript expression.')
      end
      a(Struct(1).subs{:}) = subsasg2(a(Struct(1).subs{:}),Struct(2:end),rhs);      
   case '{}'
      a{Struct(1).subs{:}} = subsasg2(a{Struct(1).subs{:}},Struct(2:end),rhs);      
   otherwise
      error('Invalid type field.')
   end
end

% end subsasg2
