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