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

%   Authors: Karen Gondoly, 5-14-98
%   Revised: Adam DiVergilio, 11-99
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.17 $  $Date: 2000/08/29 22:08:24 $

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  
SHH = get(0,'ShowHiddenHandles');
set(0,'ShowHiddenHandles','on');
NyqLine = findobj(DispAx,'Tag','NyquistLines','Visible','on');
GMv1 = findobj(DispAx(1),'Tag','GainMarginVerticalLine','Visible','on');
GMv2 = findobj(DispAx(2),'Tag','GainMarginVerticalLine','Visible','on');
PMv1 = findobj(DispAx(1),'Tag','PhaseMarginVerticalLine','Visible','on');
PMv2 = findobj(DispAx(2),'Tag','PhaseMarginVerticalLine','Visible','on');
set(0,'ShowHiddenHandles',SHH)

%---Set axis lim mode
if AutoX,
   set(DispAx(:),'XlimMode','auto','XtickMode','auto')
end
if AutoY,
   set([NyqLine(:);GMv1;GMv2;PMv1;PMv2],'visible','off')
   set(DispAx(:),'YlimMode','auto','YtickMode','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
   Xextent = datext(findobj(DispAx(1),'Tag','LTIresponseLines','Visible','on'),'x','log');
   Xmin = Xextent(1);
   Xmax = max([LastXtick ; Xextent(2)]);
   if AutoY,
      Xlims = {[Xmin Xmax]};
   else
      %---Take into account auto X-limits when not all data is shown
      aXlims = get(DispAx(:),{'Xlim'});
      aXlims = cat(1,aXlims{:});
      Xlims{1}(1) = max(Xmin,min(aXlims(:,1)));
      Xlims{1}(2) = min(Xmax,max(aXlims(:,2)));
   end
end % if XlimMode=='auto'

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

%---Adjust Y ticks (make them multiple of 45 degrees)
set(DispAx(2),'YtickMode','auto')
Yticks = get(DispAx(2),'YTick');
Yextent = datext([findobj(DispAx(2),'Tag','LTIresponseLines') ; ...
      findall(DispAx(2),'Tag','PhaseMarginLine')],'y');
[NewTicks,NewLims] = phaseticks(Yticks,Ylims{2},Yextent);
set(DispAx(2),'YTick',NewTicks)
if AutoY
   % Adjust Y limits so that new ticks extend to plot edges
   Ylims{2} = NewLims;
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
if ~isempty(NyqLine)
   Parent = get(NyqLine,{'Parent'});
   PYlims  = get(cat(1,Parent{:}),{'Ylim'});
   set(NyqLine,{'Ydata'},PYlims,'Visible','on')
end

%---Set Y extent for gain and phase margin lines
y0 = unitconv(0,'db',RespObj.MagnitudeUnits);
if ~isempty(GMv1)
   set(GMv1,'Ydata',[Ylims{1}(1) y0],'Visible','on')
   for j=1:length(GMv2)
      % Need FOR loop in HOLD ON mode
      GmPhaseLine = get(GMv2(j),'Ydata');
      set(GMv2(j),'Ydata',[GmPhaseLine(1) Ylims{2}(2)],'Visible','on')
   end
end

if ~isempty(PMv1)
   set(PMv1,'Ydata',[Ylims{1}(1) y0],'Visible','on')
   for j=1:length(PMv2)
      PmPhaseLine = get(PMv2(j),'Ydata');
      set(PMv2(j),'Ydata',[PmPhaseLine(1) Ylims{2}(2)],'Visible','on')
   end
end
