function RespObj = axeslims(RespObj)
%AXESLIMS  Sets the axes limits for Impulse Response Objects
%
%   RespObj = AXESLIMS(RespObj)

%   Authors: Karen Gondoly, 5-14-98 
%   Revised: Adam DiVergilio, 11-99
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.12 $  $Date: 2000/06/14 20:10:50 $

%---Get key response properties
AllRespProps = get(RespObj,'Undocumented');
DispAx = AllRespProps.PlotAxes;  
Xlims = AllRespProps.Xlim;
Ylims = AllRespProps.Ylim;
AutoX = strcmp(AllRespProps.XlimMode,'auto');
AutoY = strcmp(AllRespProps.YlimMode,'auto');

%---Find visible DC lines and response characteristic lines 
SHH = get(0,'ShowHiddenHandles');
set(0,'ShowHiddenHandles','on');
DCLines = findobj(DispAx,'tag','ExtraTimeLine','Visible','on');
PRLines = findobj(DispAx,'Tag','PeakResponseMarker','Visible','on','Marker','none');
SetLines = findobj(DispAx,'Tag','SettlingTimeMarker','Visible','on','Marker','none');
set(0,'ShowHiddenHandles',SHH)

%---Set axis lim mode
if AutoX,
   set(DCLines,'Xdata',[0 0])   % Don't hide DC lines as they play a role in Y limit setting
   set(DispAx(:),'XlimMode','auto')
end
if AutoY,
   %--Hide extra lines as they may interfer with Y limit computation when making  
   %  some models invisible (e.g., sys = tf(5,[1 1]), step(sys,-sys), hide 2nd 
   %  model with settling time on)
   set([PRLines;SetLines],'visible','off')
   set(DispAx(:),'YlimMode','auto')
end  

%---Adjust X-limits (set uniform X limits across all plots)
if AutoX,
   %---Determine X data extent for visible response lines
   %   RE: In held mode, this may vary between models
   onSys = strcmpi(AllRespProps.SystemVisibility,'on');
   
   %--- Determine Xmax from data
   Lines = findobj(DispAx(:),'Tag','LTIresponseLines','Visible','on');
   Xdata = get(Lines,{'Xdata'});
   Xdata = cat(2,Xdata{:});
   Xmax = max(Xdata(:));
   Xmin = 0;

   %---Clip X data at settling time (if system data is available)
   if ~all(onSys) & strcmpi(get(AllRespProps.Parent,'Tag'),'ResponseGUI'),
      Xmax = LocalGetSettlingTime(RespObj,Xmin,Xmax,onSys);
   end
   
   %---Take into account auto X-limits when YlimMode is manual (not all data is shown)
   if ~AutoY | isempty(Xmax),
      aXlims = get(DispAx(1,:),{'Xlim'});
      aXlims = cat(1,aXlims{:});
      Xmin = max([Xmin,min(aXlims(:,1))]);
      Xmax = min([Xmax,max(aXlims(:,2))]);
   else
      %---Use limits that give nice tickmarks
      Xmax = tchop(Xmax);
   end
   
   Xlims(:) = {[Xmin Xmax]};   
end 

%---Adjust Y limits
if AutoY,
   %---Set Uniform Y-axis limits across each row
   %---Enforce uniform Y-axis limits across each row
   for ctax=1:size(DispAx,1),
      Ylims{ctax} = findlims(DispAx(ctax,:),'Ylim');
   end 
end 

%---Toggle grid
RespObj = plotgrid(RespObj);

% Update axis limits in RespObj
% RE: Leave XlimMode and YlimMode untouched
RespObj = respfcn('setxylims',RespObj,Xlims,Ylims);

%---Rescale the ExtraTime Lines
if AutoX,
   % SET below only valid in auto mode (when all Xlims are equal)
   set(DCLines,'Xdata',Xlims{1})
end

%---Rescale the Peak Response Lines
for ctL=1:length(PRLines),
   X = get(PRLines(ctL),'Xdata');
   Y = get(PRLines(ctL),'Ydata');
   if ~isequal(Y(1),Y(2)),
      Ylim = get(get(PRLines(ctL),'Parent'),'Ylim');
      set(PRLines(ctL),'Ydata',[Ylim(1),Y(2)]);
   elseif ~isequal(X(1),X(2)),
      Xlim = get(get(PRLines(ctL),'Parent'),'Xlim');
      set(PRLines(ctL),'Xdata',[Xlim(1),X(2)]);
   end
end % for ctL

%---Rescale the Settling Time Lines
for ctL=1:length(SetLines),
   X = get(SetLines(ctL),'Xdata');
   Y = get(SetLines(ctL),'Ydata');
   if ~isequal(Y(1),Y(2)),
      Ylim = get(get(SetLines(ctL),'Parent'),'Ylim');
      set(SetLines(ctL),'Ydata',[Ylim(1),Y(2)]);
   elseif ~isequal(X(1),X(2)),
      Xlim = get(get(SetLines(ctL),'Parent'),'Xlim');
      set(SetLines(ctL),'Xdata',Xlim);
   end
end % for ctL

set([PRLines;SetLines],'visible','on')


%-------------------------Internal Functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% LocalGetSettlingTime %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Xmax = LocalGetSettlingTime(RespObj,Xmin,Xmax,onSys);

ST = get(RespObj,'SettlingTimeValue');
OnTime = ST(onSys);
XmaxTemp = 0;
for ct=1:length(OnTime),
   XmaxTemp = max([XmaxTemp ; OnTime(ct).SettlingTime(:)]);
end

if ~isinf(XmaxTemp) & ~isnan(XmaxTemp) & XmaxTemp>Xmin;
   %---Don't allow Infs, Nans, or zeros!
   Xmax = min(Xmax,ceil(1.05 * XmaxTemp)); % Go out 5% past settling time
end
