function [sys,sysname,PlotStyle,T,Tdata] = sysirdec(na,varargin)
%SYSIRDEC   Decodes the input list for IMPULSE AND STEP

%   Copyright 1986-2000 The MathWorks, Inc.
%   $Revision: 1.3 $  $Date: 2000/06/15 11:40:04 $

ni = length(varargin);
no = nargout;
newarg=[];inpn=[];
Tdata = [];
% Parse input list
for j=1:ni
   if isa(varargin{j},'lti')
      newarg = [newarg,{idss(varargin{j})}];
      inpn = [inpn,{inputname(j+1)}]; 
   elseif isa(varargin{j},'frd')|isa(varargin{j},'idfrd')
      error('Frequency responses cannot be used in IMPULSE  and STEP.')
   elseif isa(varargin{j},'iddata');
      model = impulse(varargin{j},'pw',na);
      ut = pvget(model,'Utility');
      Tdata = ut.impulse.time;
      newarg = [newarg,{model}];
      inpn = [inpn,{inputname(j+1)}]; 
   else      
      newarg=[newarg,varargin(j)];
      inpn = [inpn,{inputname(j+1)}];
   end
end
varargin = newarg;ni = length(varargin);
inputname = inpn;
nsys = 0;       % counts LTI systems
nstr = 0;      % counts plot style strings 
nw = 0;
sys = cell(1,ni);
sysname = cell(1,ni);
PlotStyle = cell(1,ni);
m=inf;
lastsyst=0;lastplot=0;

for j=1:ni,
   argj = varargin{j};
   if isa(argj,'idmodel') 
      if ~isempty(argj)
         if isaimp(argj)
            ut = pvget(argj,'Utility');
            Tdata = ut.impulse.time;
         end
         
         nsys = nsys+1;   
         sys{nsys} = argj;
         sysname{nsys} = inputname{j};lastsyst=j;
      end
   elseif isa(argj,'char')&j>1&(isa(varargin{j-1},'idmodel'))
      %if ~any(strcmp(lower(argj(1)),{'s','a','p'})) % to cut off a/p and 'same'
      nstr = nstr+1;   
      PlotStyle{nsys} = argj; lastplot=j;
      %end
   end
end
if lastplot==lastsyst+1,lastsyst=lastsyst+1;end
kk=1;
newarg=[];
if ni>lastsyst
   for j=lastsyst+1:ni
      newarg{kk}= varargin{j};kk=kk+1;
   end
else
   newarg={};
end
sys = sys(1:nsys);
sysname = sysname(1:nsys);
PlotStyle = PlotStyle(1:nsys);
% Now dissect newarg
if length(newarg)>1
   error('Arguments ',newarg{:} ,' not understood.')
elseif length(newarg)==1
   T = newarg{1};
else 
   T = [];
end
