tomwalters@0
|
1 function hout=suptitle(str)
|
tomwalters@0
|
2 %SUPTITLE Puts a title above all subplots.
|
tomwalters@0
|
3 % SUPTITLE('text') adds text to the top of the figure
|
tomwalters@0
|
4 % above all subplots (a "super title"). Use this function
|
tomwalters@0
|
5 % after all subplot commands.
|
tomwalters@0
|
6
|
tomwalters@0
|
7 % Drea Thomas 6/15/95 drea@mathworks.com
|
tomwalters@0
|
8 % John Cristion 12/13/00 modified
|
tomwalters@0
|
9 % Mark Histed 03/13/04 histed@mit.edu: fix disappearing legend on last plot
|
tomwalters@0
|
10 %
|
tomwalters@0
|
11 % $Id: suptitle.m 3 2006-01-10 14:50:30Z Tom $
|
tomwalters@0
|
12
|
tomwalters@0
|
13 % Warning: If the figure or axis units are non-default, this
|
tomwalters@0
|
14 % will break.
|
tomwalters@0
|
15
|
tomwalters@0
|
16
|
tomwalters@0
|
17
|
tomwalters@0
|
18 % Parameters used to position the supertitle.
|
tomwalters@0
|
19
|
tomwalters@0
|
20 % Amount of the figure window devoted to subplots
|
tomwalters@0
|
21 plotregion = .92;
|
tomwalters@0
|
22
|
tomwalters@0
|
23 % Y position of title in normalized coordinates
|
tomwalters@0
|
24 titleypos = .95;
|
tomwalters@0
|
25
|
tomwalters@0
|
26 % Fontsize for supertitle
|
tomwalters@0
|
27 %fs = get(gcf,'defaultaxesfontsize')+4;
|
tomwalters@0
|
28
|
tomwalters@0
|
29 fs = get(gcf,'defaultaxesfontsize');
|
tomwalters@0
|
30
|
tomwalters@0
|
31 % Fudge factor to adjust y spacing between subplots
|
tomwalters@0
|
32 fudge=1;
|
tomwalters@0
|
33
|
tomwalters@0
|
34 haold = gca;
|
tomwalters@0
|
35 figunits = get(gcf,'units');
|
tomwalters@0
|
36
|
tomwalters@0
|
37 % Get the (approximate) difference between full height (plot + title
|
tomwalters@0
|
38 % + xlabel) and bounding rectangle.
|
tomwalters@0
|
39
|
tomwalters@0
|
40 if (~strcmp(figunits,'pixels')),
|
tomwalters@0
|
41 set(gcf,'units','pixels');
|
tomwalters@0
|
42 pos = get(gcf,'position');
|
tomwalters@0
|
43 set(gcf,'units',figunits);
|
tomwalters@0
|
44 else,
|
tomwalters@0
|
45 pos = get(gcf,'position');
|
tomwalters@0
|
46 end
|
tomwalters@0
|
47 ff = (fs-4)*1.27*5/pos(4)*fudge;
|
tomwalters@0
|
48
|
tomwalters@0
|
49 % The 5 here reflects about 3 characters of height below
|
tomwalters@0
|
50 % an axis and 2 above. 1.27 is pixels per point.
|
tomwalters@0
|
51
|
tomwalters@0
|
52 % Determine the bounding rectange for all the plots
|
tomwalters@0
|
53
|
tomwalters@0
|
54 % h = findobj('Type','axes');
|
tomwalters@0
|
55
|
tomwalters@0
|
56 % findobj is a 4.2 thing.. if you don't have 4.2 comment out
|
tomwalters@0
|
57 % the next line and uncomment the following block.
|
tomwalters@0
|
58
|
tomwalters@0
|
59 h = findobj(gcf,'Type','axes'); % Change suggested by Stacy J. Hills
|
tomwalters@0
|
60
|
tomwalters@0
|
61 % If you don't have 4.2, use this code instead
|
tomwalters@0
|
62 %ch = get(gcf,'children');
|
tomwalters@0
|
63 %h=[];
|
tomwalters@0
|
64 %for i=1:length(ch),
|
tomwalters@0
|
65 % if strcmp(get(ch(i),'type'),'axes'),
|
tomwalters@0
|
66 % h=[h,ch(i)];
|
tomwalters@0
|
67 % end
|
tomwalters@0
|
68 %end
|
tomwalters@0
|
69
|
tomwalters@0
|
70
|
tomwalters@0
|
71
|
tomwalters@0
|
72
|
tomwalters@0
|
73 max_y=0;
|
tomwalters@0
|
74 min_y=1;
|
tomwalters@0
|
75
|
tomwalters@0
|
76 oldtitle =0;
|
tomwalters@0
|
77 for i=1:length(h),
|
tomwalters@0
|
78 if (~strcmp(get(h(i),'Tag'),'suptitle')),
|
tomwalters@0
|
79 pos=get(h(i),'pos');
|
tomwalters@0
|
80 if (pos(2) < min_y), min_y=pos(2)-ff/5*3;end;
|
tomwalters@0
|
81 if (pos(4)+pos(2) > max_y), max_y=pos(4)+pos(2)+ff/5*2;end;
|
tomwalters@0
|
82 else,
|
tomwalters@0
|
83 oldtitle = h(i);
|
tomwalters@0
|
84 end
|
tomwalters@0
|
85 end
|
tomwalters@0
|
86
|
tomwalters@0
|
87 if max_y > plotregion,
|
tomwalters@0
|
88 scale = (plotregion-min_y)/(max_y-min_y);
|
tomwalters@0
|
89 for i=1:length(h),
|
tomwalters@0
|
90 pos = get(h(i),'position');
|
tomwalters@0
|
91 pos(2) = (pos(2)-min_y)*scale+min_y;
|
tomwalters@0
|
92 pos(4) = pos(4)*scale-(1-scale)*ff/5*3;
|
tomwalters@0
|
93 set(h(i),'position',pos);
|
tomwalters@0
|
94 end
|
tomwalters@0
|
95 end
|
tomwalters@0
|
96
|
tomwalters@0
|
97 np = get(gcf,'nextplot');
|
tomwalters@0
|
98 set(gcf,'nextplot','add');
|
tomwalters@0
|
99 if (oldtitle),
|
tomwalters@0
|
100 delete(oldtitle);
|
tomwalters@0
|
101 end
|
tomwalters@0
|
102 ha=axes('pos',[0 1 1 1],'visible','off','Tag','suptitle');
|
tomwalters@0
|
103 ht=text(.5,titleypos-1,str);set(ht,'horizontalalignment','center','fontsize',fs);
|
tomwalters@0
|
104 set(gcf,'nextplot',np);
|
tomwalters@0
|
105 axes(haold);
|
tomwalters@0
|
106
|
tomwalters@0
|
107 % fix legend if one exists
|
tomwalters@0
|
108 legH = legend;
|
tomwalters@0
|
109 if ~isempty(legH)
|
tomwalters@0
|
110 axes(legH);
|
tomwalters@0
|
111 end
|
tomwalters@0
|
112
|
tomwalters@0
|
113 if nargout,
|
tomwalters@0
|
114 hout=ht;
|
tomwalters@0
|
115 end
|
tomwalters@0
|
116
|