-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtitlebetter.m
55 lines (48 loc) · 1.6 KB
/
titlebetter.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
% Place a 'title' as an axis object centered above the axis/axes in input.
% Primarily useful for cases where you want a title that spans multiple
% subplots. useplotboxpos (default 1) centers on the plot boxes rather than
% the subplot positions.
% t = titlebetter(titlestr,[ax],[useplotboxpos]);
function t = titlebetter(titlestr,ax,useplotboxpos);
if ieNotDefined('ax')
ax = gca;
end
if ieNotDefined('useplotboxpos')
useplotboxpos = 1;
end
if useplotboxpos
posfun = @plotboxpos;
else
posfun = @(ax)get(ax,'position');
end
nax = length(ax);
if nax > 1
pos = cell2mat(arrayfun(posfun,ax,'uniformoutput',false)');
else
pos = posfun(ax);
end
% top position is the tallest available ax
top = max(pos(:,2)+pos(:,4));
% Get midpoint of each ax
middles = pos(:,1) + (pos(:,3)/2);
% Assume you want the width of the title ax to be about the same as the axes
% you're plotting
%width = median(pos(:,3));
width = 0.001;
% while the height should be very modest
%height = median(pos(:,4))/5;
height = 0.001;
% So now the desired left/bottom position is...
left = median(middles) - (width/2);
bottom = top - (height/2);
tax = axes('position',[left bottom width height],...
'plotboxaspectratiomode','auto');
% May have to also disable background but nervous about Matlab and transparency
% Hm, actually text never clips outside ax so we could even make ax very very small indeed
axis off
oldu = get(tax,'units');
set(tax,'units','normalized');
t = text(.5,.5,titlestr,'horizontalalignment','center','verticalalignment',...
'bottom');
% Return to original units to prevent print weirdness
set(tax,'units',oldu);