Code covered by the BSD License  

Highlights from
colorbarlabel

from colorbarlabel by Eduardo Gonzalez Rodriguez
puts a label on a colorbar at the user given location

colorbarlabel(label, poslab, hcb)
function hcbl = colorbarlabel(label, poslab, hcb)
% hcbl = colorbarlabel(label, poslab, hcb)
% This function puts a label on a colorbar at the user given location
%
% The input arguments are:
% label = string label
% hcb = colorbar handle, if ommited a new colorbar is created
% poslab = E, W, N or S, axis where label will be localizated, default is E
% Where: E = east, W = west, N = north and S = south, 
%        E and W are on y axis and N and S are on x axis
%
% The output handle hcbl is used to manipulate font properties
%
% Examples: hcbl = colorbarlabel('temperature', 'E', hcb),
%           hcbl = colorbarlabel('temperature', 'E') or 
%           hcbl = colorbarlabel('temperature')
%
% The above examples produce the same result.
%
% If poslab is not a valid case E will be used.
%
% EGR 200905
% CICESE LA PAZ
% egonzale@cicese.mx


if nargin == 1 || nargin == 2
    %Remove previus colorbar if it exists
    colorbar('delete')
    hcb = colorbar;
    if nargin == 1
        poslab = 'E';
    end
end


%Verify uppercase letter and correct wrong cases
poslab = upper(poslab);
if (strcmp(poslab, 'E') || strcmp(poslab, 'W') || ...
        strcmp(poslab, 'N') || strcmp(poslab, 'S') ) ~= 1
    disp('Wrong position, using the default East')
    poslab = 'E';
end

%Applay the label at the given position
switch poslab
    case 'E'
    set(hcb, 'YAxisLocation', 'Right')
    hcbl = get(hcb, 'Ylabel');%Children of hc
    set(hcbl, 'String', label)
    case 'W'
    set(hcb, 'YAxisLocation', 'Left')
    hcbl = get(hcb, 'Ylabel');%Children of hc
    set(hcbl, 'String', label)
    case 'S'
    set(hcb, 'XAxisLocation', 'Bottom')
    hcbl = get(hcb, 'Xlabel');%Children of hc
    set(hcbl, 'String', label)
    case 'N'
    set(hcb, 'XAxisLocation', 'Top')
    hcbl = get(hcb, 'Xlabel');%Children of hc
    set(hcbl, 'String', label)
end

if nargout == 0
    clear hcbl %clear hcbl if it is not needed
end

Contact us at files@mathworks.com