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

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

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');
SystemVis = strcmp(AllRespProps.SystemVisibility,'on');
LocusData = RespObj.Data;

%---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 X limits if XlimMode = auto
if AutoX,
   Xlims = VisLims({LocusData(SystemVis).Xlim},AllRespProps.SelectedModels(SystemVis));
   if isempty(Xlims),
      % No visible model, or specified K vector
      Xlims = get(DispAx,'Xlim');
      % Make sure limits include (0,0)
      Xextent = max(1,Xlims(2)-Xlims(1));
      Xlims(1) = min(Xlims(1),-0.05*Xextent);
      Xlims(2) = max(Xlims(2),0.05*Xextent);
   end
   if GridOn & GridZ
      Xlims(1) = min(Xlims(1),-1);
      Xlims(2) = max(Xlims(2),1);
   end
   Xlims = {Xlims};
end

%---Update Y limits if YlimMode = auto
if AutoY,
   %---Compute desirable Y limits for visible models. Take smallest
   %   of auto HG limits and desirable limits
   Ylims = VisLims({LocusData(SystemVis).Ylim},AllRespProps.SelectedModels(SystemVis));
   if isempty(Ylims)
      Ylims = get(DispAx,'Ylim');
      Ymax = max(abs(Ylims));
      Ylims = [-Ymax Ymax];
   end
   if GridOn & GridZ
      Ylims(1) = min(Ylims(1),-1);
      Ylims(2) = max(Ylims(2),1);
   end
   Ylims = {Ylims};
end

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

%---Redraw grid if it 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');


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function Lims = VisLims(LimData,ModelVis)
% LimData is a Nx1 cell array of Mjx2 arrays where
%    * N is the number of visible systems
%    * Mj is the number of models in the j-th visible systems

Lims = zeros(0,2);

for i=1:length(LimData),
   if ~isempty(LimData{i})
      VisModLims = LimData{i}(ModelVis{i}(:),:);
      Lims = [min([Lims(:,1) ; min(VisModLims(:,1))]) ,...
            max([Lims(:,2) ; max(VisModLims(:,2))])];
   end
end
