function LsimRespObj = lsimplot(RespObj,Systems,X,Y,PlotStyle);
%LSIMPLOT Create a Linear Simulation Response Object for the Control System Toolbox

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

ni = nargin;

%---Properties
LsimRespObj = struct('InputSignal',[]);

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

%---Initialize LsimRespObj
LsimFlag = isa(RespObj,'lsimplot');
if LsimFlag
   LsimRespObj = RespObj;
   RespObj = LsimRespObj.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 LsimFlag
   % Adding to existing LSIMPLOT object
   RH0 = get(RespObj,'ResponseHandles');
else
   % New LSIMPLOT 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

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

for ctR=1:length(ResponseHandles)  % each 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}(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','Linear Simulation Results',...
      'Ylabel','Amplitude',...
      'Xlabel','Time (sec)'};

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

else
   %---No Plot Option Menus for LSIM responses opened at the command line
   set(ContextMenu.PlotOptions.Main,'visible','off');
   set(ContextMenu.GridMenu,'Separator','on')
   RespObj = pvset(RespObj,'ResponseHandles',ResponseHandles,...
      'UIContextMenu',ContextMenu,BackAxLabels{:});
   LsimRespObj = class(LsimRespObj,'lsimplot',RespObj);
end

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