function RespObj = axeslims(RespObj)
%AXESLIMS  Sets the axes limits for singular value plot
%
%   RespObj = AXESLIMS(RespObj)

%   Authors: Karen Gondoly, 1-30-98
%   Revised: Adam DiVergilio, 11-99
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.16 $  $Date: 2000/06/14 20:04:08 $

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 Nyquist lines and response characteristic lines 
SHH = get(0,'ShowHiddenHandles');
set(0,'ShowHiddenHandles','on');
NyqLine = findobj(DispAx,'Tag','NyquistLines','Visible','on');
PRLines = findobj(DispAx,'Tag','PeakResponseMarker','Visible','on','Marker','none');
set(0,'ShowHiddenHandles',SHH)

%---Set axis lim mode
if AutoX,
   set(DispAx(:),'XlimMode','auto')
end
if AutoY,
   set([NyqLine;PRLines],'visible','off')
   set(DispAx(:),'YlimMode','auto')
end  

%---Adjust X-limits
if AutoX,
   % Compute first tick past largest Nyquist frequency
   % (to show at least one tick past largest visible Nyquist frequency)
   if ~isempty(NyqLine)
      nfs = get(NyqLine,{'Xdata'});
      nfs = cat(1,nfs{:});
      nfmax = max(nfs(:,1));             % largest Nyquist freq
      nfdec = 10^floor(log10(nfmax));    % corresponding decade
      nftick = round(nfmax/nfdec);
      LastXtick = max(nftick+1,(nftick>6)*10) * nfdec;
   else
      LastXtick = [];
   end
   
   %---Make sure X-limits do not exceed range of data
   Lines = findall(DispAx(1,1),'Tag','LTIresponseLines','Visible','on'); 
   Xdata = get(Lines,{'Xdata'}); 
   if ~isempty(Xdata), 
      Xdata = cat(2,Xdata{:}); 
      Xmin = min(Xdata(:)); 
      Xmax = max([LastXtick ; Xdata(:)]); 
   else
      Xmin = 0.1;
      Xmax = 10;
   end
   
   %---Take into account auto X-limits when YlimMode is manual (not all data is shown)
   if ~AutoY,
      Xlim = get(DispAx,'Xlim');
      Xmin = max(Xmin,Xlim(1));
      Xmax = min(Xmax,Xlim(2));
   end
   
   Xlims = {[Xmin Xmax]};
end % if XlimMode=='auto'

%---Adjust Y limits
if AutoY,
   Ylims = {findlims(DispAx(1),'Ylim')};
end 

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

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

%---Make sure Nyquist line spans entire Ylimit range;
set(NyqLine,'Ydata',Ylims{1})

%---Make sure Peak Response lines span the X and Y limits
for ctL=1:length(PRLines),
   X = get(PRLines(ctL),'Xdata');
   Y = get(PRLines(ctL),'Ydata');
   if Y(1)~=Y(2),
      % Vertical line
      set(PRLines(ctL),'Ydata',[Ylims{1}(1) Y(2)])
   else
      % Horizontal line
      set(PRLines(ctL),'Xdata',[Xlims{1}(1) X(2)])
   end
end % for ctL

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