function RespObj = axeslims(RespObj)
%AXESLIMS  Sets the axes limits for Pole-Zero Response Objects
%
%   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:08:42 $

AllRespProps = get(RespObj,'Undocumented');
DispAx = AllRespProps.PlotAxes;  
Xlims = AllRespProps.Xlim;
Ylims = AllRespProps.Ylim;
AutoX = strcmp(AllRespProps.XlimMode,'auto');
AutoY = strcmp(AllRespProps.YlimMode,'auto');
GridOn = strcmp(AllRespProps.Grid,'on');
GridZ = strcmp(AllRespProps.GridType,'zgrid');

%---Find axis lines and hide them (may interfer with axis limits)
SHH = get(0,'ShowHiddenHandles');
set(0,'ShowHiddenHandles','on');
XaxisLines = findobj(DispAx,'Tag','PZxaxisLine');
YaxisLines = findobj(DispAx,'Tag','PZyaxisLine');
set(0,'ShowHiddenHandles',SHH)
GridLines = RespObj.GridLines;
GridLines = GridLines(ishandle(GridLines));

%---Set axis lim mode
if AutoX,
   set([XaxisLines;GridLines],'Visible','off')
   set(DispAx,'XlimMode','auto')
end
if AutoY,
   set([YaxisLines;GridLines],'Visible','off')
   set(DispAx,'YlimMode','auto')
end  

%---Update limits for axis with auto lim mode
if AutoY,
   Ylims = get(DispAx,'Ylim');
   % Enforce symmetry (when all models are invisible HG picks [0 1] instead of [-1 1]
   Ymax = max(abs(Ylims));
   Ylims = [-Ymax Ymax];
   if GridOn & GridZ 
      Ylims(1) = min(Ylims(1),-1);
      Ylims(2) = max(Ylims(2),1);
   end
   Ylims = {Ylims};
end

if AutoX,
   Xlims = get(DispAx,'Xlim');
   %---Make sure imaginary axis is shown
   Extent = max([1,Xlims(2)-Xlims(1),2*Ylims{1}(2)]);
   Xlims(1) = min(Xlims(1),-0.05*Extent);
   Xlims(2) = max(Xlims(2),0.05*Extent);
   if GridOn & GridZ
      Xlims(1) = min(Xlims(1),-1);
      Xlims(2) = max(Xlims(2),1);
   end
   Xlims = {Xlims};
end


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

% Redraw grid and update limits if grid is on
if GridOn
   RespObj = plotgrid(RespObj);   
end

%---Adjust axis line extent
set(XaxisLines,'Xdata',Xlims{1},'Visible','on');
set(YaxisLines,'Ydata',Ylims{1},'Visible','on');
