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