2.2 Controlling Axis Labels and Titles

Problem

When attributes of the xlabel, ylabel, zlabel, and title are changed, the changes are ignored when the Figure Window is resized or printed. For example:

>> xlabel('X-Axis')
>> hx = get(gca,'XLabel');
>> set(hx,'Rotation',-90)
If the Figure Window is resized or printed, the xlabel will revert to the default rotation of 0 degrees.

Solution

When objects are added to the axes or the Figure Window is resized or printed, the axes are rerendered. This causes the axis labels and titles to reset to their default property values. To avoid this you must replace the axis labels and titles with text objects. Below is an M-file which uses text objects to label each axis and adds titles to plots:

**********************************************************
function handle = label(lab,ax)
% LABEL: Places an axis label or title that can be 
% controlled by the user.
%
% LABEL(LAB) uses the text contained in LAB as the X-axis 
% label.
%
% LABEL(LAB,AX) labels the axis or title defined by AX.
% AX can be one of the following:
%
% 'x' or 'X'      ==>     X-axis
% 'y' or 'Y'      ==>     Y-axis
% 'z' or 'Z'      ==>     Z-axis
% 't' or 'T'      ==>     Title
%
% HANDLE = LABEL(...) returns the handle to the label.
%
% EXAMPLES:
%
% >> label('X-Axis','x')
% >> label('Y-Axis','y')
% >> label('Z-Axis','z')
% >> label('Title','t')
%
% SEE ALSO: text, title, xlabel, ylabel, and zlabel
if nargin == 1
  ax = 'x';
end
if ~isstr(lab) | ~isstr(ax)
  error('Inputs must be a string variable')
end
% Determine the axis or title to be labeled
if strcmp(ax,'x') | strcmp(ax,'X')
  H = get(gca,'xlabel');
elseif strcmp(ax,'y') | strcmp(ax,'Y')
  H = get(gca,'ylabel');
elseif strcmp(ax,'z') | strcmp(ax,'Z')
  H = get(gca,'zlabel');
elseif strcmp(ax,'t') | strcmp(ax,'T')
  H = get(gca,'title');
else
  error('Invalid axis')
end
% Replace the axis label or title with a TEXT string
T = text('color',         get(H,'color'), ...
    'erasemode',          get(H,'erasemode'), ...
    'fontangle',          get(H,'fontangle'), ...
    'fontname',           get(H,'fontname'), ...
    'fontsize',           get(H,'fontsize'), ...
    'fontstrikethrough',  get(H,'fontstrikethrough'), ...
    'fontunderline',      get(H,'fontunderline'), ...
    'fontweight',         get(H,'fontweight'), ...
    'horizontalalignment',get(H,'horizontalalignment'), ...
    'position',           get(H,'position'), ...
    'rotation',           get(H,'rotation'), ...
    'string',             lab, ...
    'units',              get(H,'units'), ...
    'verticalalignment',  get(H,'verticalalignment'), ...
    'buttondownfcn',      get(H,'buttondownfcn'), ...
    'clipping',           get(H,'clipping'), ...
    'interruptible',      get(H,'interruptible'),...
    'userdata',           get(H,'userdata'), ...
    'visible',            get(H,'visible'));
set(T,'units','normal')
% Required for printing
%  Remove the original axis label
delete(H);
% Output
if nargout
  handle = T;
end

(c) Copyright 1994 by The MathWorks, Inc.