function labeltog(RespObj)
%LABELTOG  Adjust visibility of PlotAxes labels and ticks.

%   Authors: P. Gahinet and K. Gondoly
%   Revised: A. DiVergilio
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.11 $ $Date: 2000/06/14 20:07:29 $

% Identify visible axes and selected I/O channels
[OnY,OnU,Ysel,Usel] = axisvis(RespObj);
VisAxes = RespObj.PlotAxes(OnY,OnU);

% Quick exit if no axes are visible
if isempty(VisAxes)
   return
end

% Determine Y label and title visibility based on axis
% grouping, number of selected channels, and plot type
AxesGrouping = RespObj.AxesGrouping;
ResponseType = RespObj.ResponseType;
IsMargin = strcmp(ResponseType,'margin');
Yvis = (~any(strcmp(ResponseType,{'sigma' 'pzmap' 'margin' 'rlocus'})) & ...
      (any(strcmp(AxesGrouping,{'none','inputs'})) | length(Ysel)==1));
Tvis = (~any(strcmp(ResponseType,{'sigma' 'initial' 'pzmap' 'lsim' 'rlocus'})) & ...
      (any(strcmp(AxesGrouping,{'none','outputs'})) | length(Usel)==1));

%---Do not show any labels for SISO plots with default IO names
ShowLabels = 1;
if length(RespObj.InputLabel)==1 & length(RespObj.OutputLabel)==1 & ~IsMargin
   if strcmpi(RespObj.InputLabel,'U(1)') & strcmpi(RespObj.OutputLabel,'Y(1)')
      ShowLabels = 0;
   end
end

% Set visible Y labels 
Y = get(VisAxes,{'Ylabel'});
Y = reshape(cat(1,Y{:}),size(VisAxes));
set(Y,'Visible','off','String','')
if Yvis & ShowLabels
   % Update visible labels
   if any(strcmp(RespObj.ResponseType,{'bode','margin'}))
      Ysel = Ysel(2:2:end)/2;
      %---Show IO labels for phase plots
       YP = Y(2:2:end,:);
       LocalLabelOutputs(YP(:,1),RespObj.OutputLabel(Ysel))
       set(YP(:,1),'visible','on')
      %---Show IO labels for mag plots
       Y = Y(1:2:end-1,:);
   end
   LocalLabelOutputs(Y(:,1),RespObj.OutputLabel(Ysel))
   set(Y(:,1),'visible','on')
end

% Set visible titles (U labels)
T = get(VisAxes,{'Title'});
T = reshape(cat(1,T{:}),size(VisAxes));
if IsMargin
   set(T,'Visible','off')
else
   set(T,'Visible','off','String','')
end
if Tvis & ShowLabels
   if ~IsMargin
      LocalLabelInputs(T(1,:),RespObj.InputLabel(Usel))
   end
   set(T(1,:),'visible','on')
end

% Delete unwanted tick marks
set(VisAxes(1:end-1,:),'Xticklabel',[]);
set(VisAxes(:,2:end),'Yticklabel',[])

% Activate auto tick labels on border plots
set(VisAxes(end,:),'XtickLabelMode','auto')
set(VisAxes(:,1),'YtickLabelMode','auto')

%---Reposition Title/XYLabels
labelpos(RespObj);


%---------------------------Internal functions---------------------------
%%%%%%%%%%%%%%%%%%%%%%%%
%%% LocalLabelInputs %%%
%%%%%%%%%%%%%%%%%%%%%%%%
function LocalLabelInputs(TitleHandles,InputLabels)

% Default set up
set(TitleHandles,'Visible','off');

% Set title strings
set(TitleHandles,{'String'},strcat({xlate('From: ')},InputLabels));


%%%%%%%%%%%%%%%%%%%%%%%%%
%%% LocalLabelOutputs %%%
%%%%%%%%%%%%%%%%%%%%%%%%%
function LocalLabelOutputs(YlabelHandles,OutputLabels)

% Default set up
set(YlabelHandles,'Visible','off');

% Set Y label strings
set(YlabelHandles,{'String'},strcat({xlate('To: ')},OutputLabels));
