function result = subsref(sys,Struct)
%SUBSREF Referencing for Transfer Function objects.
%          Sys(Outputs,Inputs)
%          Sys.fieldname
%          Sys.fieldname(indices) 
%          Sys.fieldname(i,j)
%
%       See also:  GET, SSDATA, TFDATA, ZPKDATA.

%       Author(s): A. Potvin, 3-1-94, 12-1-95
%       Revised: P. Gahinet, 4-1-96
%       Copyright (c) 1986-97 by The MathWorks, Inc.
%       $Revision: 1.3 $  $Date: 1997/04/09 00:30:00 $

% Effect on LTI properties: all inherited

ni = nargin;
if ni==1,
   result = sys;
   return
end

StructL = length(Struct);
switch Struct(1).type
case '.'
   % At least sys.fieldname
   % Output will be a (piece of) one of the system fields
   result = get(sys,Struct(1).subs);
   if StructL>1,
      result = subsref2(result,Struct(2:end));
   end
   return
case '()'
   % sys(something)
   if StructL>1,
      error('Inconsistently placed () in subscript expression')
   end
   % Fall through to code below
case '{}'
   error('Cell contents reference from a non-cell array object')
otherwise
   error(['Unknown type: ' Struct(1).type])
end


% Everything else just handles sys(something)
indices = Struct(1).subs;
lind = length(indices);
[P,M] = size(sys);

% Perform some error checking
if lind>2,
   error('Index exceeds matrix dimensions.')
elseif lind==0,
   error('Missing variable or function.')
elseif lind==1,
   % Absolute indexing
   if min(P,M)>1, 
     error('Use double indexing for MIMO systems, as in SYS(i,j)');
   elseif P==1,    % MISO
     indrow = ':';   indcol = indices{1};
   else            % SIMO
     indrow = indices{1};   indcol = ':';
   end
else
   indrow = indices{1};    indcol = indices{2};
end

if (~isstr(indrow) & (max(indrow)>P | any(indrow<=0))) | ...
   (~isstr(indcol) & (max(indcol)>M | any(indcol<=0))),
   error('Index exceeds system dimensions');
end


% Set NUM and DEN
result = sys;
result.num = sys.num(indices{:});
result.den = sys.den(indices{:});

% Set LTI properties
L = struct(sys.lti);    % LTI structure
L.InputName = L.InputName(indcol,1);
L.OutputName = L.OutputName(indrow,1);
if L.Ts==0,  L.Td = L.Td(1,indcol);  end
result.lti = quickset(sys.lti,L,'d');

% end ../@tf/subsref.m
