function labelpos(RespObj,scale);
%LABELPOS  Repositions BackgroundAxes labels.
           
%   Authors: A. DiVergilio
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.9 $  $Date: 2000/06/14 20:07:29 $

if nargin<2
   scale = 1;
end

%---Do nothing if the figure is being deleted
%--- (prevents loss of warning state due to invalid handles during delete)
if strcmpi(get(RespObj.Parent,'BeingDeleted'),'on'), return; end

WarnState = warning; warning off;  %---Hide warning incase label pos goes negative on log plots

tshave = 20*scale;  %---Top of background axes to top of plotaxes
yshave = 12*scale;  %---Left edge of background axes to left edge of plotaxes
space  = 16*scale;  %---Space between subplots within single response object
cushion = 2*scale;  %---Extra cushion between labels and plots
xcushion = 0*scale; %---Extra cushion for xlabel
ycushion = 2*scale; %---Extra cushion for ylabel
tcushion = 6*scale; %---Extra cushion for title

%---Label units
 Lu = get(RespObj.Handles.Title,'Units');

%---Pixel position of BackgroundAxes
 ba = RespObj.BackgroundAxes;
 BAou = get(ba,'Units');
 if strcmp(BAou,'pixels')
    BAp = get(ba,'Position');
 else
    BAop = get(ba,'Position');
    set(ba,'Units','pixels');
    BAp = get(ba,'Position');
    set(ba,'Units',BAou,'Position',BAop);
 end

%---PlotAxes
 pa = RespObj.PlotAxes;
 pa = pa(:);

%---Find best YLabel position
 yl = get(pa,'YLabel');
 if iscell(yl), yl = cat(1,yl{:}); end
 op = get(yl,'Position');
 set(yl,'Units','pixels');
 ex = get(yl,'Extent');
 if iscell(ex), ex = cat(1,ex{:}); end
 pos = min(ex(:,1)) - cushion - ycushion;
 if length(yl)>1, set(yl,{'Units'},{'data'},{'Position'},op);
 else,            set(yl,'Units','data','Position',op);
 end

 if isempty(get(RespObj.Handles.Ylabel(2),'String'))
    set(RespObj.Handles.Ylabel(1),...
       'Units','pixels',...
       'HorizontalAlignment','center',...
       'Position',[yshave+pos (BAp(4)-tshave)/2 0],'Units',Lu);
 else
    yex1 = get(RespObj.Handles.Ylabel(1),'Extent');
    yex2 = get(RespObj.Handles.Ylabel(2),'Extent');
    aex = (BAp(4)-tshave-space)/2;
    if (yex1(4)<=aex) & (yex2(4)<=aex)
       HA1 = 'center';
       YP1 = aex/2;
       HA2 = 'center';
       YP2 = aex+aex/2+space;
    else
       HA1 = 'right';
       YP1 = aex;
       HA2 = 'left';
       YP2 = aex+space;
    end
    set(RespObj.Handles.Ylabel(1),...
       'Units','pixels','HorizontalAlignment',HA1,'Position',[yshave+pos YP1 0],'Units',Lu);
    set(RespObj.Handles.Ylabel(2),...
       'Units','pixels','HorizontalAlignment',HA2,'Position',[yshave+pos YP2 0],'Units',Lu);
 end

%---Find best XLabel position
 xl = get(pa,'XLabel');
 if iscell(xl), xl = cat(1,xl{:}); end
 op = get(xl,'Position');
 set(xl,'Units','pixels');
 ex = get(xl,'Extent');
 if iscell(ex), ex = cat(1,ex{:}); end
 pos = min(ex(:,2)) - cushion - xcushion;
 if length(xl)>1, set(xl,{'Units'},{'data'},{'Position'},op);
 else,            set(xl,'Units','data','Position',op);
 end
 set(RespObj.Handles.Xlabel,...
    'Units','pixels','Position',[(BAp(3)+yshave)/2 pos 0],'Units',Lu);

%---Find best Title position
 tl = get(pa,'Title');
 if iscell(tl), tl = cat(1,tl{:}); end
 op = get(tl,'Position');
 set(tl,'Units','pixels');
 ex = get(tl,'Extent');
 if iscell(ex), ex = cat(1,ex{:}); end
 pos = max(ex(:,4)) + cushion + tcushion;
 if length(tl)>1, set(tl,{'Units'},{'data'},{'Position'},op);
 else,            set(tl,'Units','data','Position',op);
 end
 set(RespObj.Handles.Title,...
    'Units','pixels','Position',[(BAp(3)+yshave)/2 BAp(4)-tshave+pos 0],'Units',Lu);

warning(WarnState);
