Home > bioelectromagnetism > suptitle_withpatch.m

suptitle_withpatch

PURPOSE ^

SUPTITLE Puts a title above all subplots.

SYNOPSIS ^

function hout=suptitle(str)

DESCRIPTION ^

SUPTITLE Puts a title above all subplots.
    SUPTITLE('text') adds text to the top of the figure
    above all subplots (a "super title"). Use this function
    after all subplot commands.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function hout=suptitle(str)
0002 %SUPTITLE Puts a title above all subplots.
0003 %    SUPTITLE('text') adds text to the top of the figure
0004 %    above all subplots (a "super title"). Use this function
0005 %    after all subplot commands.
0006 
0007 % Drea Thomas 6/15/95 drea@mathworks.com
0008 % John Cristion 12/13/00 modified
0009 % Mark Histed 03/13/04 histed@mit.edu: fix disappearing legend on last plot
0010 %
0011 % $Id: suptitle_withpatch.m,v 1.1 2005/06/23 20:42:38 psdlw Exp $
0012 
0013 % Warning: If the figure or axis units are non-default, this
0014 % will break.
0015 
0016 
0017 
0018 % Parameters used to position the supertitle.
0019 
0020 % Amount of the figure window devoted to subplots
0021 plotregion = .92;
0022 
0023 % Y position of title in normalized coordinates
0024 titleypos  = .95;
0025 
0026 % Fontsize for supertitle
0027 %fs = get(gcf,'defaultaxesfontsize')+4;
0028 
0029 fs = get(gcf,'defaultaxesfontsize');
0030 
0031 % Fudge factor to adjust y spacing between subplots
0032 fudge=1;
0033 
0034 haold = gca;
0035 figunits = get(gcf,'units');
0036 
0037 % Get the (approximate) difference between full height (plot + title
0038 % + xlabel) and bounding rectangle.
0039 
0040     if (~strcmp(figunits,'pixels')),
0041         set(gcf,'units','pixels');
0042         pos = get(gcf,'position');
0043         set(gcf,'units',figunits);
0044     else,
0045         pos = get(gcf,'position');
0046     end
0047     ff = (fs-4)*1.27*5/pos(4)*fudge;
0048 
0049         % The 5 here reflects about 3 characters of height below
0050         % an axis and 2 above. 1.27 is pixels per point.
0051 
0052 % Determine the bounding rectange for all the plots
0053 
0054 % h = findobj('Type','axes');
0055 
0056 % findobj is a 4.2 thing.. if you don't have 4.2 comment out
0057 % the next line and uncomment the following block.
0058     
0059 h = findobj(gcf,'Type','axes');  % Change suggested by Stacy J. Hills
0060 
0061 % If you don't have 4.2, use this code instead
0062 %ch = get(gcf,'children');
0063 %h=[];
0064 %for i=1:length(ch),
0065 %  if strcmp(get(ch(i),'type'),'axes'),
0066 %    h=[h,ch(i)];
0067 %  end
0068 %end
0069 
0070     
0071 
0072 
0073 max_y=0;
0074 min_y=1;
0075 
0076 oldtitle =0;
0077 for i=1:length(h),
0078     if (~strcmp(get(h(i),'Tag'),'suptitle')),
0079         pos=get(h(i),'pos');
0080         if (pos(2) < min_y), min_y=pos(2)-ff/5*3;end;
0081         if (pos(4)+pos(2) > max_y), max_y=pos(4)+pos(2)+ff/5*2;end;
0082     else,
0083         oldtitle = h(i);
0084     end
0085 end
0086 
0087 if max_y > plotregion,
0088     scale = (plotregion-min_y)/(max_y-min_y);
0089     for i=1:length(h),
0090         pos = get(h(i),'position');
0091         pos(2) = (pos(2)-min_y)*scale+min_y;
0092         pos(4) = pos(4)*scale-(1-scale)*ff/5*3;
0093         set(h(i),'position',pos);
0094     end
0095 end
0096 
0097 np = get(gcf,'nextplot');
0098 set(gcf,'nextplot','add');
0099 if (oldtitle),
0100     delete(oldtitle);
0101 end
0102 ha=axes('pos',[0 1 1 1],'visible','off','Tag','suptitle');
0103 ht=text(.5,titleypos-1,str);set(ht,'horizontalalignment','center','fontsize',fs);
0104 set(gcf,'nextplot',np);
0105 axes(haold);
0106 
0107 % fix legend if one exists
0108 legH = legend;
0109 if ~isempty(legH)
0110     axes(legH);
0111 end
0112 
0113 if nargout,
0114     hout=ht;
0115 end
0116

Generated on Mon 15-Aug-2005 15:36:19 by m2html © 2003