Home > matlabmk > cbar_topo.m

cbar_topo

PURPOSE ^

function [handle]=cbar_topo(type,colors,minmax, grad)

SYNOPSIS ^

function [handle]=cbar_topo(type,colors,minmax,grad)

DESCRIPTION ^

function [handle]=cbar_topo(type,colors,minmax, grad)

 cbar() - Display full or partial color bar

 Usage:
    >> cbar % create a vertical cbar on the right side of a figure
    >> cbar(type) % specify direction as 'vert' or 'horiz'
    >> cbar(type,colors) % specify which colormap colors to plot
  else
    >> cbar(axhandle) % specify the axes to draw cbar in

    >> h = cbar(type|axhandle,colors, minmax, grad)

 Inputs:
  type      - ['vert'|'horiz'] direction of the cbar {default: 'vert')
              ELSE axhandle = handle of axes to draw the cbar
  colors    - vector of colormap indices to display, 
              (else int n -> display colors [1:end-n]) {default: all}
  minmax    - [min, max] range of values to label on colorbar 
  grad      - [integer] number of color graduations. {default: 5}.

 Example:
         >> colormap('default') % default colormap is 64-color 'jet'
         >> cbar('vert',33:64); % plot a vertical cbar colored green->red 
                                % useful for showing >0 (warm) and 0 (green) 
                                % values only in a green=0 plot

 Author: Colin Humphries, Arnaud Delorme, CNL / Salk Institute, Feb. 1998-

 See also: colorbar()

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 %function [handle]=cbar_topo(type,colors,minmax, grad)
0002 %
0003 % cbar() - Display full or partial color bar
0004 %
0005 % Usage:
0006 %    >> cbar % create a vertical cbar on the right side of a figure
0007 %    >> cbar(type) % specify direction as 'vert' or 'horiz'
0008 %    >> cbar(type,colors) % specify which colormap colors to plot
0009 %  else
0010 %    >> cbar(axhandle) % specify the axes to draw cbar in
0011 %
0012 %    >> h = cbar(type|axhandle,colors, minmax, grad)
0013 %
0014 % Inputs:
0015 %  type      - ['vert'|'horiz'] direction of the cbar {default: 'vert')
0016 %              ELSE axhandle = handle of axes to draw the cbar
0017 %  colors    - vector of colormap indices to display,
0018 %              (else int n -> display colors [1:end-n]) {default: all}
0019 %  minmax    - [min, max] range of values to label on colorbar
0020 %  grad      - [integer] number of color graduations. {default: 5}.
0021 %
0022 % Example:
0023 %         >> colormap('default') % default colormap is 64-color 'jet'
0024 %         >> cbar('vert',33:64); % plot a vertical cbar colored green->red
0025 %                                % useful for showing >0 (warm) and 0 (green)
0026 %                                % values only in a green=0 plot
0027 %
0028 % Author: Colin Humphries, Arnaud Delorme, CNL / Salk Institute, Feb. 1998-
0029 %
0030 % See also: colorbar()
0031 
0032 %123456789012345678901234567890123456789012345678901234567890123456789012
0033 
0034 % Copyright (C) Colin Humphries, CNL / Salk Institute, Feb. 1998
0035 %
0036 % This program is free software; you can redistribute it and/or modify
0037 % it under the terms of the GNU General Public License as published by
0038 % the Free Software Foundation; either version 2 of the License, or
0039 % (at your option) any later version.
0040 %
0041 % This program is distributed in the hope that it will be useful,
0042 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0043 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0044 % GNU General Public License for more details.
0045 %
0046 % You should have received a copy of the GNU General Public License
0047 % along with this program; if not, write to the Free Software
0048 % Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
0049 
0050 % $Log: cbar.m,v $
0051 % Revision 1.5  2006/02/16 21:21:06  arno
0052 % same
0053 %
0054 % Revision 1.4  2006/02/16 21:20:35  arno
0055 % new option pos
0056 %
0057 % Revision 1.3  2004/05/07 16:07:49  scott
0058 % help comments - added a useful example -sm
0059 %
0060 % Revision 1.2  2003/07/30 01:53:13  arno
0061 % adding grad option
0062 %
0063 % Revision 1.1  2002/04/05 17:36:45  jorn
0064 % Initial revision
0065 %
0066 
0067 % 12-13-98 added minmax arg -Scott Makeig
0068 % 01-25-02 reformated help & license, added links -ad
0069 
0070 function [handle]=cbar_topo(type,colors,minmax,grad)
0071 
0072 if nargin < 2
0073   colors = 0;
0074 end
0075 posscale = 'off';
0076 if nargin < 1
0077   type = 'vert';
0078   ax = [];
0079 else
0080   if isempty(type)
0081     type = 0;
0082   end
0083   if type(1) == 0
0084     ax = [];
0085     type = 'vert';
0086   elseif strcmpi(type, 'pos')
0087     ax = [];
0088     type = 'vert';
0089     posscale = 'on';
0090   else      
0091     if isstr(type)
0092       ax = [];
0093     else
0094       ax = type;
0095       type = [];
0096     end
0097   end
0098 end
0099 
0100 if nargin>2
0101   if size(minmax,1) ~= 1 | size(minmax,2) ~= 2
0102     help cbar
0103     fprintf('cbar() : minmax arg must be [min,max]\n');
0104     return
0105   end
0106 end
0107 if nargin < 4
0108     grad = 5;
0109 end;
0110 
0111 %obj = findobj('tag','cbar','parent',gcf);
0112 %if ~isempty(obj) & ~isempty(arg)
0113 %  arg = [];
0114 %  ax = obj;
0115 %end
0116 
0117 %%%%%%%%%%%%%%%%%%%%%%%%%%%
0118 % Choose colorbar position
0119 %%%%%%%%%%%%%%%%%%%%%%%%%%%
0120 
0121 if (length(colors) == 1) & (colors == 0)
0122   t = caxis;
0123 else
0124   t = [0 1];
0125 end
0126 if ~isempty(type)
0127   if strcmp(type,'vert')  
0128     cax = gca;
0129     pos = get(cax,'Position');
0130     stripe = 0.04; 
0131     edge = 0.01;
0132     space = .02;
0133 
0134 %    set(cax,'Position',[pos(1) pos(2) pos(3)*(1-stripe-edge-space) pos(4)])
0135 %    rect = [pos(1)+(1-stripe-edge)*pos(3) pos(2) stripe*pos(3) pos(4)];
0136 
0137     set(cax,'Position',[pos(1) pos(2) pos(3) pos(4)])
0138 %    rect = [pos(1)+pos(3)+space pos(2) stripe*pos(3) pos(4)];
0139     rect = [pos(1)+pos(3)+space pos(2)+pos(4)*.2 stripe*pos(3) pos(4)*.6];
0140     ax = axes('Position', rect);
0141   elseif strcmp(type,'horiz')
0142     cax = gca;
0143     pos = get(cax,'Position');
0144     stripe = 0.075; 
0145     space = .1;  
0146     set(cax,'Position',...
0147         [pos(1) pos(2)+(stripe+space)*pos(4) pos(3) (1-stripe-space)*pos(4)])
0148     rect = [pos(1) pos(2) pos(3) stripe*pos(4)];
0149     ax = axes('Position', rect);
0150   end
0151 else
0152   pos = get(ax,'Position');
0153   if pos(3) > pos(4)
0154     type = 'horiz';
0155   else
0156     type = 'vert';
0157   end
0158 end
0159   
0160 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0161 % Draw colorbar using image()
0162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0163 
0164 map = colormap;
0165 n = size(map,1);
0166 
0167 if length(colors) == 1
0168   if strcmp(type,'vert')
0169       if strcmpi(posscale, 'on')
0170           image([0 1],[0 t(2)],[ceil(n/2):n-colors]');
0171       else
0172           image([0 1],t,[1:n-colors]');
0173       end;
0174       set(ax,'xticklabelmode','manual')
0175       set(ax,'xticklabel',[],'YAxisLocation','right')
0176       
0177   else
0178     image(t,[0 1],[1:n-colors]);
0179     set(ax,'yticklabelmode','manual')
0180     set(ax,'yticklabel',[],'YAxisLocation','right')
0181   end
0182   set(ax,'Ydir','normal','YAxisLocation','right')
0183 
0184 else % length > 1
0185 
0186   if max(colors) > n
0187     error('Color vector excedes size of colormap')
0188   end
0189   if strcmp(type,'vert')
0190     image([0 1],t,[colors]');
0191     set(ax,'xticklabelmode','manual')
0192     set(ax,'xticklabel',[])
0193   else
0194     image([0 1],t,[colors]);
0195     set(ax,'yticklabelmode','manual')
0196     set(ax,'yticklabel',[],'YAxisLocation','right')
0197   end  
0198   set(ax,'Ydir','normal','YAxisLocation','right')
0199 end
0200 
0201 %%%%%%%%%%%%%%%%%%%%%%%%%
0202 % Adjust cbar ticklabels
0203 %%%%%%%%%%%%%%%%%%%%%%%%%
0204 
0205 if nargin > 2 
0206   Cax = get(ax,'Ylim');
0207   CBTicks = [Cax(1):(Cax(2)-Cax(1))/(grad-1):Cax(2)]; % caxis tick positions
0208   CBLabels = [minmax(1):(minmax(2)-minmax(1))/(grad-1):minmax(2)]; % tick labels
0209   dec = floor(log10(max(abs(minmax)))); % decade of largest abs value
0210   CBLabels = ([minmax]* [ linspace(1,0, grad);linspace(0, 1, grad)]);
0211   %[1.0 .75 .50 .25 0.0; 0.0 .25 .50 .75 1.0]);
0212   if dec<1
0213     CBLabels = round(CBLabels*10^(1-dec))*10^(dec-1);
0214   elseif dec == 1
0215     CBLabels = round(CBLabels*10^(2-dec))*10^(dec-2);
0216   else
0217     CBLabels = round(CBLabels);
0218   end
0219 % minmax
0220 % CBTicks
0221 % CBLabels
0222 
0223   if strcmp(type,'vert')
0224     set(ax,'Ytick',CBTicks);
0225     set(ax,'Yticklabel',CBLabels);
0226   else
0227     set(ax,'Xtick',CBTicks);
0228     set(ax,'Xticklabel',CBLabels);
0229   end
0230 end
0231 handle = ax;
0232 
0233 %%%%%%%%%%%%%%%%%%
0234 % Adjust cbar tag
0235 %%%%%%%%%%%%%%%%%%
0236 
0237 set(ax,'tag','cbar')

Generated on Tue 10-May-2016 16:37:45 by m2html © 2005