[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 |
|
---|
[14461] | 25 | %Get plot axes
|
---|
| 26 | mainaxes = gca;
|
---|
| 27 |
|
---|
[14402] | 28 | %process options
|
---|
| 29 | options = pairoptions(varargin{:});
|
---|
[14417] | 30 | if exist(options,'tick') & exist(options,'ticksep'),
|
---|
| 31 | error('only one of tick or ticksep can be specified');
|
---|
| 32 | end
|
---|
[14402] | 33 | fontsize = getfieldvalue(options,'fontsize',12);
|
---|
| 34 | smallbars = getfieldvalue(options,'smallbars',false);
|
---|
| 35 |
|
---|
| 36 | %Colorbar position
|
---|
| 37 | if ~exist(options,'position'),
|
---|
| 38 | position = plotboxpos;
|
---|
| 39 | xstart = position(1)+position(3)+0.01;
|
---|
| 40 | ystart = position(2);
|
---|
| 41 | width = .02;
|
---|
| 42 | height = position(4);
|
---|
| 43 | else
|
---|
| 44 | position = getfieldvalue(options,'position');
|
---|
| 45 | xstart = position(1);
|
---|
| 46 | ystart = position(2);
|
---|
| 47 | width = position(3);
|
---|
| 48 | height = position(4);
|
---|
| 49 | end
|
---|
| 50 | axes('Units','normalized','Position',[xstart ystart width height],'XTickLabel','','YTickLabel','','Visible','on');
|
---|
| 51 | xlim([0 1]);
|
---|
| 52 | ylim([0 1]);
|
---|
| 53 |
|
---|
[14417] | 54 | %Prepare ticks
|
---|
| 55 | deltaz = getfieldvalue(options,'ticksep',dtick(zmax-zmin));
|
---|
| 56 | ztick = getfieldvalue(options,'tick',(deltaz*ceil(zmin/deltaz)):deltaz:zmax);
|
---|
| 57 | if (any(ztick>zmax) | any(ztick<zmin)),
|
---|
| 58 | error('one or more specified tick values falls outside of [zmin,zmax]');
|
---|
| 59 | end
|
---|
[14402] | 60 | ytick = (ztick-zmin)/(zmax-zmin);
|
---|
| 61 |
|
---|
[14417] | 62 | %Display colorbar
|
---|
[14402] | 63 | hold on
|
---|
| 64 | if strcmpi(getfieldvalue(options,'orientation','vertical'),'vertical'),
|
---|
| 65 | image_rgb = ind2rgb(repmat(uint16(1:length(cmap))',1,2),cmap);
|
---|
| 66 | else
|
---|
| 67 | image_rgb = ind2rgb(repmat(uint16(1:length(cmap))',1,2)',cmap);
|
---|
| 68 | end
|
---|
| 69 | imagesc([0 1],[0 1],image_rgb);
|
---|
| 70 | patch([0,0,1,1],[0,1,1,0],'k','FaceColor','none','Clipping','off')
|
---|
| 71 |
|
---|
| 72 | %Add ticks
|
---|
| 73 | if strcmpi(getfieldvalue(options,'orientation','vertical'),'vertical'),
|
---|
[14462] | 74 | %Use FOR LOOP otherwise numbers are not correcly centered
|
---|
| 75 | for i=1:length(ytick), text(1.5,ytick(i),num2str(ztick(i)),'HorizontalAlignment','left','VerticalAlignment','middle','FontSize',fontsize); end
|
---|
[14402] | 76 | if smallbars,
|
---|
| 77 | for i=1:numel(ztick)
|
---|
| 78 | patch([0.8 1.0],[ytick(i) ytick(i)],'k')
|
---|
| 79 | patch([0.0 0.2],[ytick(i) ytick(i)],'k')
|
---|
| 80 | end
|
---|
| 81 | end
|
---|
| 82 | else
|
---|
[14461] | 83 | %Use FOR LOOP otherwise numbers are not correcly centered
|
---|
| 84 | for i=1:length(ytick), text(ytick(i),-0.5,num2str(ztick(i)),'HorizontalAlignment','center','VerticalAlignment','top','FontSize',fontsize); end
|
---|
[14402] | 85 | if smallbars,
|
---|
| 86 | for i=1:numel(ztick)
|
---|
| 87 | patch([ytick(i) ytick(i)],[0.8 1.0],[ytick(i) ytick(i)],'k')
|
---|
| 88 | patch([ytick(i) ytick(i)],[0.0 0.2],[ytick(i) ytick(i)],'k')
|
---|
| 89 | end
|
---|
| 90 | end
|
---|
| 91 | end
|
---|
| 92 |
|
---|
| 93 | if exist(options,'title'),
|
---|
[14490] | 94 | if strcmpi(getfieldvalue(options,'orientation','vertical'),'horizontal'),
|
---|
| 95 | th=title(getfieldvalue(options,'title'),'FontSize',fontsize);
|
---|
| 96 | set(th,'Position',[ytick(end)+0.075,-0.3]);
|
---|
| 97 | else
|
---|
| 98 | title(getfieldvalue(options,'title'),'FontSize',fontsize);
|
---|
| 99 | end
|
---|
[14402] | 100 | end
|
---|
[14461] | 101 | if exist(options,'ylabel'),
|
---|
| 102 | ylabel(getfieldvalue(options,'ylabel'),'FontSize',fontsize);
|
---|
| 103 | end
|
---|
[14402] | 104 |
|
---|
[14461] | 105 | %Back to original axes
|
---|
[15211] | 106 | if strcmpi(getfieldvalue(options,'showregion',0),0)
|
---|
| 107 | axes(mainaxes);
|
---|
| 108 | end
|
---|
[14461] | 109 |
|
---|
[14402] | 110 | function delta = dtick(range)
|
---|
| 111 | %Tick intervals
|
---|
| 112 | m = 10^floor(log10(range));
|
---|
| 113 | p = ceil(range/m);
|
---|
| 114 | if p <= 1, delta = .1*m;
|
---|
| 115 | elseif p == 2, delta = .2*m;
|
---|
| 116 | elseif p <= 5, delta = .5*m;
|
---|
| 117 | else delta = m;
|
---|
| 118 | end
|
---|