tomwalters@0: function hout=suptitle(str) tomwalters@0: %SUPTITLE Puts a title above all subplots. tomwalters@0: % SUPTITLE('text') adds text to the top of the figure tomwalters@0: % above all subplots (a "super title"). Use this function tomwalters@0: % after all subplot commands. tomwalters@0: tomwalters@0: % Drea Thomas 6/15/95 drea@mathworks.com tomwalters@0: % John Cristion 12/13/00 modified tomwalters@0: % Mark Histed 03/13/04 histed@mit.edu: fix disappearing legend on last plot tomwalters@0: % tomwalters@0: % $Id: suptitle.m 3 2006-01-10 14:50:30Z Tom $ tomwalters@0: tomwalters@0: % Warning: If the figure or axis units are non-default, this tomwalters@0: % will break. tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: % Parameters used to position the supertitle. tomwalters@0: tomwalters@0: % Amount of the figure window devoted to subplots tomwalters@0: plotregion = .92; tomwalters@0: tomwalters@0: % Y position of title in normalized coordinates tomwalters@0: titleypos = .95; tomwalters@0: tomwalters@0: % Fontsize for supertitle tomwalters@0: %fs = get(gcf,'defaultaxesfontsize')+4; tomwalters@0: tomwalters@0: fs = get(gcf,'defaultaxesfontsize'); tomwalters@0: tomwalters@0: % Fudge factor to adjust y spacing between subplots tomwalters@0: fudge=1; tomwalters@0: tomwalters@0: haold = gca; tomwalters@0: figunits = get(gcf,'units'); tomwalters@0: tomwalters@0: % Get the (approximate) difference between full height (plot + title tomwalters@0: % + xlabel) and bounding rectangle. tomwalters@0: tomwalters@0: if (~strcmp(figunits,'pixels')), tomwalters@0: set(gcf,'units','pixels'); tomwalters@0: pos = get(gcf,'position'); tomwalters@0: set(gcf,'units',figunits); tomwalters@0: else, tomwalters@0: pos = get(gcf,'position'); tomwalters@0: end tomwalters@0: ff = (fs-4)*1.27*5/pos(4)*fudge; tomwalters@0: tomwalters@0: % The 5 here reflects about 3 characters of height below tomwalters@0: % an axis and 2 above. 1.27 is pixels per point. tomwalters@0: tomwalters@0: % Determine the bounding rectange for all the plots tomwalters@0: tomwalters@0: % h = findobj('Type','axes'); tomwalters@0: tomwalters@0: % findobj is a 4.2 thing.. if you don't have 4.2 comment out tomwalters@0: % the next line and uncomment the following block. tomwalters@0: tomwalters@0: h = findobj(gcf,'Type','axes'); % Change suggested by Stacy J. Hills tomwalters@0: tomwalters@0: % If you don't have 4.2, use this code instead tomwalters@0: %ch = get(gcf,'children'); tomwalters@0: %h=[]; tomwalters@0: %for i=1:length(ch), tomwalters@0: % if strcmp(get(ch(i),'type'),'axes'), tomwalters@0: % h=[h,ch(i)]; tomwalters@0: % end tomwalters@0: %end tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: max_y=0; tomwalters@0: min_y=1; tomwalters@0: tomwalters@0: oldtitle =0; tomwalters@0: for i=1:length(h), tomwalters@0: if (~strcmp(get(h(i),'Tag'),'suptitle')), tomwalters@0: pos=get(h(i),'pos'); tomwalters@0: if (pos(2) < min_y), min_y=pos(2)-ff/5*3;end; tomwalters@0: if (pos(4)+pos(2) > max_y), max_y=pos(4)+pos(2)+ff/5*2;end; tomwalters@0: else, tomwalters@0: oldtitle = h(i); tomwalters@0: end tomwalters@0: end tomwalters@0: tomwalters@0: if max_y > plotregion, tomwalters@0: scale = (plotregion-min_y)/(max_y-min_y); tomwalters@0: for i=1:length(h), tomwalters@0: pos = get(h(i),'position'); tomwalters@0: pos(2) = (pos(2)-min_y)*scale+min_y; tomwalters@0: pos(4) = pos(4)*scale-(1-scale)*ff/5*3; tomwalters@0: set(h(i),'position',pos); tomwalters@0: end tomwalters@0: end tomwalters@0: tomwalters@0: np = get(gcf,'nextplot'); tomwalters@0: set(gcf,'nextplot','add'); tomwalters@0: if (oldtitle), tomwalters@0: delete(oldtitle); tomwalters@0: end tomwalters@0: ha=axes('pos',[0 1 1 1],'visible','off','Tag','suptitle'); tomwalters@0: ht=text(.5,titleypos-1,str);set(ht,'horizontalalignment','center','fontsize',fs); tomwalters@0: set(gcf,'nextplot',np); tomwalters@0: axes(haold); tomwalters@0: tomwalters@0: % fix legend if one exists tomwalters@0: legH = legend; tomwalters@0: if ~isempty(legH) tomwalters@0: axes(legH); tomwalters@0: end tomwalters@0: tomwalters@0: if nargout, tomwalters@0: hout=ht; tomwalters@0: end tomwalters@0: