# v_cblabel

## PURPOSE

V_CBLABEL add a label to a colorbar C=(L,H)

## SYNOPSIS

function c=v_cblabel(l,h)

## DESCRIPTION

    V_CBLABEL add a label to a colorbar C=(L,H)

     Usage: (1) imagesc(...)
                colorbar;
                v_cblabel('Colorbar label');       % label the closest colorbar to the current axis

            (2) imagesc(...)
                ch=colorbar;
                v_cblabel('Colorbar label',ch);    % label the explicitly specified colorbar

     Inputs:

         l        Label string for colorbar
         h        Handle of the colorbar, axis or figure [default = current axis]

     Outputs:

         c        Handle of the colorbar

          Copyright (C) Mike Brookes 2000-2024
          Version: $Id: v_cblabel.m 10865 2018-09-21 17:22:45Z dmb $

       VOICEBOX is a MATLAB toolbox for speech processing.
       Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       This program is free software; you can redistribute it and/or modify
       it under the terms of the GNU Lesser General Public License as published by
       the Free Software Foundation; either version 3 of the License, or
       (at your option) any later version.

       This program is distributed in the hope that it will be useful,
       but WITHOUT ANY WARRANTY; without even the implied warranty of
       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       GNU Lesser General Public License for more details.

       You can obtain a copy of the GNU Lesser General Public License from
       https://www.gnu.org/licenses/ .

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

- v_correlogram V_CORRELOGRAM calculate correlogram [y,ty]=(x,inc,nw,nlag,m,fs)
- v_filtbankm V_FILTBANKM determine matrix for a linear/mel/erb/bark-spaced v_filterbank [X,IL,IH]=(P,N,FS,FL,FH,W)
- v_filterbank V_FILTERBANK appply v_filterbank to a signal: [y,zo]=(b,a,x,gd)
- v_gaussmixp V_GAUSSMIXP calculate probability densities from or plot a Gaussian mixture model
- v_spgrambw V_SPGRAMBW Draw spectrogram [T,F,B]=(s,fs,mode,bw,fmax,db,tinc,ann)
- v_sphrharm V_SPHRHARM forward and inverse spherical harmonic transform
- v_stftw V_STFTW converts a time-domain signal into the time-frequency domain with the Short-time Fourier Transform [Y,SO,T,F]=(X,NW,M,OV)

## SOURCE CODE

    0001 function c=v_cblabel(l,h)
    0002 %V_CBLABEL add a label to a colorbar C=(L,H)
    0003 %
    0004 % Usage: (1) imagesc(...)
    0005 %            colorbar;
    0006 %            v_cblabel('Colorbar label');       % label the closest colorbar to the current axis
    0007 %
    0008 %        (2) imagesc(...)
    0009 %            ch=colorbar;
    0010 %            v_cblabel('Colorbar label',ch);    % label the explicitly specified colorbar
    0011 %
    0012 % Inputs:
    0013 %
    0014 %     l        Label string for colorbar
    0015 %     h        Handle of the colorbar, axis or figure [default = current axis]
    0016 %
    0017 % Outputs:
    0018 %
    0019 %     c        Handle of the colorbar
    0020 %
    0021 %
    0022 %      Copyright (C) Mike Brookes 2000-2024
    0023 %      Version: $Id: v_cblabel.m 10865 2018-09-21 17:22:45Z dmb $
    0024 %
    0025 %   VOICEBOX is a MATLAB toolbox for speech processing.
    0026 %   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
    0027 %
    0028 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0029 %   This program is free software; you can redistribute it and/or modify
    0030 %   it under the terms of the GNU Lesser General Public License as published by
    0031 %   the Free Software Foundation; either version 3 of the License, or
    0032 %   (at your option) any later version.
    0033 %
    0034 %   This program is distributed in the hope that it will be useful,
    0035 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
    0036 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    0037 %   GNU Lesser General Public License for more details.
    0038 %
    0039 %   You can obtain a copy of the GNU Lesser General Public License from
    0040 %   https://www.gnu.org/licenses/ .
    0041 %
    0042 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0043 persistent t
    0044 if isempty(t)
    0045     t=[1 0 0.5 0; 0 1 0 0.5]'; % matrix used to find centre of graphical object
    0046 end
    0047 if nargin<2
    0048     h=gcf; % default is current figure
    0049 end
    0050 switch get(h,'Type')
    0051     case 'axes'
    0052         if strcmpi(get(h,'Tag'),'colorbar')
    0053             c=h;
    0054         else
    0055             cg=h.Position*t;                              % coordinates of the centre of requested axis
    0056             while ~strcmp(get(h,'Type'),'figure')           % loop to find ancestor of h of type figure
    0057                 h=get(h,'Parent');                          % find parent figure
    0058                 if h==0
    0059                     error('cannot find ancestor figure');
    0060                 end
    0061             end
    0062             cx=findobj(h,'tag','Colorbar');                 % find all colorbars on this figure
    0063             nc=length(cx);
    0064             switch nc
    0065                 case 0
    0066                     error('There is no colour bar on this figure')
    0067                 case 1
    0068                     c=cx(1);
    0069                 otherwise                                   % find the nearest colorbar to the centre of requested axis
    0070                     c=cx(1);                                % select the first colorbar
    0071                     d=sum((c.Position*t-cg).^2);            % squared distance to centre of requested axis
    0072                     for ic=2:nc                             % loop through each colorbar
    0073                         di=sum((cx(ic).Position*t-cg).^2);  % squared distance to centre of requested axis
    0074                         if di<d                             % if this colorbar is nearer
    0075                             c=cx(ic);                       % ... select this colorbar
    0076                             d=di;                           % ... and save the squared distance
    0077                         end
    0078                     end
    0079             end
    0080         end
    0081     case 'figure'
    0082         cx=findobj(h,'tag','Colorbar');
    0083         nc=length(cx);
    0084         switch nc
    0085             case 0
    0086                 error('There is no colour bar on this figure')
    0087             case 1
    0088                 c=cx(1);
    0089             otherwise                                   % find the nearest colorbar to the centre of current graph
    0090                 cg=gca().Position*t;                    % coordinates of the centre of current graph
    0091                 c=cx(1);                                % select the first colorbar
    0092                 d=sum((c.Position*t-cg).^2);            % squared distance to centre of current graph
    0093                 for ic=2:nc                             % loop through each colorbar
    0094                     di=sum((cx(ic).Position*t-cg).^2);  % squared distance to centre of current graph
    0095                     if di<d                             % if this colorbar is nearer
    0096                         c=cx(ic);                       % ... select this colorbar
    0097                         d=di;                           % ... and save the squared distance
    0098                     end
    0099                 end
    0100         end
    0101     case 'colorbar'
    0102         c=h;
    0103     otherwise
    0104         error(['h argument must be colorbar, axis or figure handle but is ''' get(h,'Type') '''']);
    0105 end
    0106 set(get(c,'ylabel'),'string',l);