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

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

AllRespProps = get(RespObj,'Undocumented');
DispAx = AllRespProps.PlotAxes;  
Xlims = AllRespProps.Xlim;
Ylims = AllRespProps.Ylim;
ZoomMode = AllRespProps.Zoom;
SISO = (length(Xlims)==1 & length(Ylims)==1);

% Find visible margin lines (SISO only)
if SISO
   MargLines = findall(DispAx(1),'Tag','StabilityMarginMarker','LineStyle','-.','Visible','on');
   MargLineH = findobj(MargLines,'UserData',0);
   MargLineV = findobj(MargLines,'UserData',180);
   set([MargLineH;MargLineV],'visible','off')
end

switch ZoomMode
case 'tight'
   % Compute desirable axis limits
   [Xlims,Ylims] = niclims(RespObj,ZoomMode);
   AutoX = 1;
   
otherwise
   % Default axis limit management
   AutoX = strcmp(AllRespProps.XlimMode,'auto');
   if AutoX,
      set(DispAx(:),'XlimMode','auto')
   end
   
   AutoY = strcmp(AllRespProps.YlimMode,'auto');
   if AutoY,
      set(DispAx(:),'YlimMode','auto')
   end  
   
   %---Adjust X and Y limits
   if AutoX,
      %---Enforce uniform X-axis limits across each row
      for ctax=1:size(DispAx,2),
         Xlims{ctax} = findlims(DispAx(:,ctax),'Xlim');
      end 
   end 
   if AutoY,
      %---Enforce uniform Y-axis limits across each row
      for ctax=1:size(DispAx,1),
         Ylims{ctax} = findlims(DispAx(ctax,:),'Ylim');
      end 
   end 
end

% Refine axis limits if SISO grid is present
if strcmp(AllRespProps.Grid,'on') 
    if SISO
        % Set X and Y axis limits to computed Xlims and Ylims
        for ctax=1:size(DispAx,2),
            set(DispAx(:,ctax),'Xlim',Xlims{ctax})
        end
        for ctax=1:size(DispAx,1),
            set(DispAx(ctax,:),'Ylim',Ylims{ctax});
        end % for ctax
        
        % Use PlotGrid to determine desirable grid limits
        RespObj = plotgrid(RespObj);
        NewLims = get(DispAx,{'Xlim';'Ylim'});
        Xlims = NewLims(1); 
        Ylims = NewLims(2);
    else
        % Reset HG grid props (for, e.g., refresh)
        set(DispAx,'Xgrid','on','Ygrid','on')
    end
end % if Grid on

%---Adjust X ticks (make them multiple of 45 degrees)
set(DispAx(:),'XtickMode','auto')
for ctax=1:size(DispAx,2),
   % Enforce computed X limits
   set(DispAx(:,ctax),'Xlim',Xlims{ctax})
   Xticks = get(DispAx(1,ctax),'XTick');
   if AutoX,
      % Auto mode. Determine data extent (visible data + visible grid lines in SISO case
      Handles = findobj(DispAx(:,ctax),'Tag','LTIresponseLines','Visible','on');
      if SISO,
         VisLines = findall(DispAx,'Type','line','Visible','on');
         % REVISIT: Contribution of StabilityMarginMarker to Xextent ignored
         % in tight mode because Xlim from NICLIMS is based only on data
         % Try ltiview('nichols',tf(1,[1 0])) + margins on
         Handles = [ Handles ; findobj(VisLines,'Tag','CSTgridLines') ; ...
               findobj(VisLines,'Tag','StabilityMarginMarker') ];
      end
      Xextent = datext(Handles,'x');
      % Adjust ticks and modify limits if necessary
      % RE: Slightly expand X limits so that phase lines flush with axis borders 
      % don't vanish. Cf ltiview('nichols',tf(1,[1 0])) + margins on + zoom out
      [NewXticks,NewLims] = phaseticks(Xticks,Xlims{ctax},Xextent);
      Xlims{ctax} = NewLims + [-0.001 0.001] * (NewLims(2)-NewLims(1));
   else
      % Fixed limit mode
      NewXticks = phaseticks(Xticks,Xlims{ctax});
   end
   set(DispAx(:,ctax),'XTick',NewXticks)
end

% Update limits and ResponseHandles property
% RE: Leaves XlimMode and YlimMode untouched
RespObj = respfcn('setxylims',RespObj,Xlims,Ylims);

%---Make sure margin lines spans entire axis range
if SISO
   set(MargLineH,'Xdata',Xlims{1},'Visible','on')
   set(MargLineV,'Ydata',Ylims{1},'Visible','on')
end
