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 General Public License as published by the Free Software Foundation; either version 2 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 General Public License for more details. You can obtain a copy of the GNU General Public License from http://www.gnu.org/copyleft/gpl.html or by writing to Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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 General Public License as published by 0031 % the Free Software Foundation; either version 2 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 General Public License for more details. 0038 % 0039 % You can obtain a copy of the GNU General Public License from 0040 % http://www.gnu.org/copyleft/gpl.html or by writing to 0041 % Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA. 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);