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