[14402] | 1 | function manualcb(zmin,zmax,cmap,varargin)
|
---|
| 2 | %MANUALCB - custom colorbar
|
---|
| 3 | %
|
---|
| 4 | % Usage:
|
---|
[14403] | 5 | % manualcb(min,max,colormap,options)
|
---|
[14402] | 6 | %
|
---|
| 7 | % Available options:
|
---|
| 8 | % - 'fontsize' : default is 12
|
---|
[14404] | 9 | % - 'smallbars' : bars next to each tick (default is false)
|
---|
[14402] | 10 | % - 'position' : colorbar position in normalized units
|
---|
| 11 | % - 'orientation' : 'vertical' (default) or 'horizontal'
|
---|
[14404] | 12 | % - 'title' : colorbar title
|
---|
[14417] | 13 | % - 'tick' : specified values of tick labels
|
---|
[14418] | 14 | % - 'ticksep' : spacing between ticks
|
---|
[14402] | 15 |
|
---|
| 16 | %check inputs
|
---|
| 17 | if nargin<3,
|
---|
[14404] | 18 | help manualcb
|
---|
[14402] | 19 | error('bad usage');
|
---|
| 20 | end
|
---|
| 21 | if zmin>zmax,
|
---|
| 22 | error('zmin should be smaller than zmax');
|
---|
| 23 | end
|
---|
| 24 |
|
---|
| 25 | %process options
|
---|
| 26 | options = pairoptions(varargin{:});
|
---|
[14417] | 27 | if exist(options,'tick') & exist(options,'ticksep'),
|
---|
| 28 | error('only one of tick or ticksep can be specified');
|
---|
| 29 | end
|
---|
[14402] | 30 | fontsize = getfieldvalue(options,'fontsize',12);
|
---|
| 31 | smallbars = getfieldvalue(options,'smallbars',false);
|
---|
| 32 |
|
---|
| 33 | %Colorbar position
|
---|
| 34 | if ~exist(options,'position'),
|
---|
| 35 | position = plotboxpos;
|
---|
| 36 | xstart = position(1)+position(3)+0.01;
|
---|
| 37 | ystart = position(2);
|
---|
| 38 | width = .02;
|
---|
| 39 | height = position(4);
|
---|
| 40 | else
|
---|
| 41 | position = getfieldvalue(options,'position');
|
---|
| 42 | xstart = position(1);
|
---|
| 43 | ystart = position(2);
|
---|
| 44 | width = position(3);
|
---|
| 45 | height = position(4);
|
---|
| 46 | end
|
---|
| 47 | axes('Units','normalized','Position',[xstart ystart width height],'XTickLabel','','YTickLabel','','Visible','on');
|
---|
| 48 | xlim([0 1]);
|
---|
| 49 | ylim([0 1]);
|
---|
| 50 |
|
---|
[14417] | 51 | %Prepare ticks
|
---|
| 52 | deltaz = getfieldvalue(options,'ticksep',dtick(zmax-zmin));
|
---|
| 53 | ztick = getfieldvalue(options,'tick',(deltaz*ceil(zmin/deltaz)):deltaz:zmax);
|
---|
| 54 | if (any(ztick>zmax) | any(ztick<zmin)),
|
---|
| 55 | error('one or more specified tick values falls outside of [zmin,zmax]');
|
---|
| 56 | end
|
---|
[14402] | 57 | ytick = (ztick-zmin)/(zmax-zmin);
|
---|
| 58 |
|
---|
[14417] | 59 | %Display colorbar
|
---|
[14402] | 60 | hold on
|
---|
| 61 | if strcmpi(getfieldvalue(options,'orientation','vertical'),'vertical'),
|
---|
| 62 | image_rgb = ind2rgb(repmat(uint16(1:length(cmap))',1,2),cmap);
|
---|
| 63 | else
|
---|
| 64 | image_rgb = ind2rgb(repmat(uint16(1:length(cmap))',1,2)',cmap);
|
---|
| 65 | end
|
---|
| 66 | imagesc([0 1],[0 1],image_rgb);
|
---|
| 67 | patch([0,0,1,1],[0,1,1,0],'k','FaceColor','none','Clipping','off')
|
---|
| 68 |
|
---|
| 69 | %Add ticks
|
---|
| 70 | if strcmpi(getfieldvalue(options,'orientation','vertical'),'vertical'),
|
---|
| 71 | text(1.5+zeros(size(ztick)),ytick,num2str(ztick'),'HorizontalAlignment','left','VerticalAlignment','middle','FontSize',fontsize)
|
---|
| 72 | if smallbars,
|
---|
| 73 | for i=1:numel(ztick)
|
---|
| 74 | patch([0.8 1.0],[ytick(i) ytick(i)],'k')
|
---|
| 75 | patch([0.0 0.2],[ytick(i) ytick(i)],'k')
|
---|
| 76 | end
|
---|
| 77 | end
|
---|
| 78 | else
|
---|
| 79 | text(ytick,-0.5+zeros(size(ztick)),num2str(ztick'),'HorizontalAlignment','center','VerticalAlignment','top','FontSize',fontsize)
|
---|
| 80 | if smallbars,
|
---|
| 81 | for i=1:numel(ztick)
|
---|
| 82 | patch([ytick(i) ytick(i)],[0.8 1.0],[ytick(i) ytick(i)],'k')
|
---|
| 83 | patch([ytick(i) ytick(i)],[0.0 0.2],[ytick(i) ytick(i)],'k')
|
---|
| 84 |
|
---|
| 85 | end
|
---|
| 86 | end
|
---|
| 87 | end
|
---|
| 88 |
|
---|
| 89 | if exist(options,'title'),
|
---|
| 90 | title(getfieldvalue(options,'title'),'FontSize',fontsize);
|
---|
| 91 | end
|
---|
| 92 |
|
---|
| 93 | function delta = dtick(range)
|
---|
| 94 | %Tick intervals
|
---|
| 95 | m = 10^floor(log10(range));
|
---|
| 96 | p = ceil(range/m);
|
---|
| 97 | if p <= 1, delta = .1*m;
|
---|
| 98 | elseif p == 2, delta = .2*m;
|
---|
| 99 | elseif p <= 5, delta = .5*m;
|
---|
| 100 | else delta = m;
|
---|
| 101 | end
|
---|