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