function ICrespObj = icplot(RespObj,Systems,X,Y,PlotStyle);      
%ICPLOT Create a Initial Condition Response Object for the Control System Toolbox

%   Authors: Karen Gondoly, 2-19-98
%   Revised: Adam W. DiVergilio
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.19 $  $Date: 2000/06/19 21:02:55 $

ni = nargin;

%---Properties
ICrespObj = struct(...
   'PeakResponse','off',...
   'PeakResponseValue',[]);

%---Return cases (ni==0,1)
switch ni
case 0
   RespObj = response;
   ICrespObj = class(ICrespObj,'icplot',RespObj);
   return
case 1
   if ~isa(RespObj,'response')
      error('Invalid Response Object passed to ICPLOT')
   elseif isa(RespObj,'icplot')
      ICrespObj = RespObj;
   else
      ICrespObj = class(ICrespObj,'icplot',RespObj);
   end
   return
end

%---Initialize ICRespObj
ICFlag = isa(RespObj,'icplot');
if ICFlag
   ICrespObj = RespObj;
   RespObj = ICrespObj.response;
else
   set(get(RespObj,'PlotAxes'),'XScale','linear','YScale','linear');
end

%---Access needed parent property values
PV = get(RespObj,{'PlotAxes','UIContextMenu','SystemNames'});
[LTIdisplayAxes,ContextMenu,SystemNames] = deal(PV{:});
if ICFlag
   % Adding to existing ICPLOT object
   RH0 = get(RespObj,'ResponseHandles');
else
   % New ICPLOT object
   RH0 = {};
end

%---Get dimension info and names of new systems
[numout,numin] = size(LTIdisplayAxes);
NumOldSys = length(RH0);                      % number of existing systems
NewSystemNames = SystemNames(NumOldSys+1:end);
NumNewSys = length(NewSystemNames);           % number of added systems 

%---Set initial values for ICPLOT-specific properties
PeakRespVals = struct('System',cell(NumNewSys,1),'Time',[],'Peak',[]);

%---Initialize Line Userdata
ResponseHandles = cell(NumNewSys,1);

for ctR=1:length(ResponseHandles)  % each new system
   % Get plot data
   Xarray = X{ctR};
   Yarray = Y{ctR};
   SizeArray = size(Xarray);
   NumArray = prod(SizeArray);

   % Get the style and legend
   Style = struct2cell(PlotStyle(ctR));
   [CC,LL,MM,LegendStr] = deal(Style{:});
   if NumArray>1
      ArrayDimsStr = sprintf('%dx',SizeArray);
      LegendStr = sprintf('%s array: %s',ArrayDimsStr(1:end-1),LegendStr);
   end

   %---Add legend to system name for new systems
   set(ContextMenu.Systems.Names(NumOldSys+ctR),...
      'Label',sprintf('%s (%s)',NewSystemNames{ctR},LegendStr),...
      'ForegroundColor',CC)

   LineHandle = cell(numout,1);
   for ctout=1:numout
      RH=cell(SizeArray);
      for ctArray=1:NumArray
         Xdata = Xarray{ctArray};
         Ydata = Yarray{ctArray};

         %---Response line UserData
         LineUd = struct('System',NewSystemNames{ctR},'Input',1,...
            'Output',ctout,'Array',rguihelper('ind2sub',SizeArray,ctArray));

         RH{ctArray}(2,1)=line([0 1],[0 0],'Parent',LTIdisplayAxes(ctout,1),'Visible','off',...
            'HandleVisibility','off','HitTest','off','UserData',LineHandle{ctout,1},...
            'Color',get(LTIdisplayAxes(ctout,1),'YColor'),'LineStyle',':','Tag','ExtraTimeLine');

         RH{ctArray}(1,1)=line(Xdata,Ydata(:,ctout),'Color',CC,'Marker',MM,'LineStyle',LL,...
            'DeleteFcn','delresp(gcbo)','parent',LTIdisplayAxes(ctout,1),'Visible','off',...
            'ButtonDownFcn','rguifcn(''showbox'',gcbf);',...
            'Tag','LTIresponseLines','UserData',LineUd);
      end
      LineHandle{ctout,1} = RH;
   end
   ResponseHandles{ctR} = LineHandle;
end

%---Set the BackgroundAxes labels
BackAxLabels = {...
      'Title','Response to Initial Conditions',...
      'Ylabel','Amplitude',...
      'Xlabel','Time (sec)'};

if ICFlag  % Append Data
   RespObj = pvset(RespObj,'UIContextMenu',ContextMenu,...
      'ResponseHandles',[RH0;ResponseHandles],...
      BackAxLabels{:});
   ICrespObj.PeakResponseValue = [ICrespObj.PeakResponseValue;PeakRespVals];
   ICrespObj.response = RespObj;

else
   %---Add the Plot Option Menus
   ContextMenu.PlotOptions.PeakResponse = uimenu(ContextMenu.PlotOptions.Main,...
      'label',xlate('Peak Response'),...
      'Callback',['menufcn(''togglepeak'')']);

   RespObj = pvset(RespObj,'ResponseHandles',ResponseHandles,...
      'UIContextMenu',ContextMenu,BackAxLabels{:});
   ICrespObj.PeakResponseValue = PeakRespVals;
   ICrespObj = class(ICrespObj,'icplot',RespObj);
end

%---Update HG pointer to Response Object
builtin('set',ContextMenu.Main,'userdata',ICrespObj)
