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