suptitle.m 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. function hout=suptitle(str)
  2. %SUPTITLE puts a title above all subplots.
  3. %
  4. % SUPTITLE('text') adds text to the top of the figure
  5. % above all subplots (a "super title"). Use this function
  6. % after all subplot commands.
  7. %
  8. % SUPTITLE is a helper function for yeastdemo.
  9. % Copyright 2003-2010 The MathWorks, Inc.
  10. % Warning: If the figure or axis units are non-default, this
  11. % will break.
  12. % Parameters used to position the supertitle.
  13. % Amount of the figure window devoted to subplots
  14. plotregion = .92;
  15. % Y position of title in normalized coordinates
  16. titleypos = .95;
  17. % Fontsize for supertitle
  18. fs = get(gcf,'defaultaxesfontsize') - 2;
  19. % Fudge factor to adjust y spacing between subplots
  20. fudge=1;
  21. haold = gca;
  22. figunits = get(gcf,'units');
  23. % Get the (approximate) difference between full height (plot + title
  24. % + xlabel) and bounding rectangle.
  25. if (~strcmp(figunits,'pixels')),
  26. set(gcf,'units','pixels');
  27. pos = get(gcf,'position');
  28. set(gcf,'units',figunits);
  29. else
  30. pos = get(gcf,'position');
  31. end
  32. ff = (fs-4)*1.27*5/pos(4)*fudge;
  33. % The 5 here reflects about 3 characters of height below
  34. % an axis and 2 above. 1.27 is pixels per point.
  35. % Determine the bounding rectangle for all the plots
  36. % h = findobj('Type','axes');
  37. % findobj is a 4.2 thing.. if you don't have 4.2 comment out
  38. % the next line and uncomment the following block.
  39. h = findobj(gcf,'Type','axes'); % Change suggested by Stacy J. Hills
  40. max_y=0;
  41. min_y=1;
  42. oldtitle = NaN;
  43. numAxes = length(h);
  44. thePositions = zeros(numAxes,4);
  45. for i=1:numAxes
  46. pos=get(h(i),'pos');
  47. thePositions(i,:) = pos;
  48. if (~strcmp(get(h(i),'Tag'),'suptitle')),
  49. if (pos(2) < min_y)
  50. min_y=pos(2)-ff/5*3;
  51. end;
  52. if (pos(4)+pos(2) > max_y)
  53. max_y=pos(4)+pos(2)+ff/5*2;
  54. end;
  55. else
  56. oldtitle = h(i);
  57. end
  58. end
  59. if max_y > plotregion,
  60. scale = (plotregion-min_y)/(max_y-min_y);
  61. for i=1:numAxes
  62. pos = thePositions(i,:);
  63. pos(2) = (pos(2)-min_y)*scale+min_y;
  64. pos(4) = pos(4)*scale-(1-scale)*ff/5*3;
  65. set(h(i),'position',pos);
  66. end
  67. end
  68. np = get(gcf,'nextplot');
  69. set(gcf,'nextplot','add');
  70. if ishghandle(oldtitle)
  71. delete(oldtitle);
  72. end
  73. axes('pos',[0 1 1 1],'visible','off','Tag','suptitle');
  74. ht=text(.5,titleypos-1,str);set(ht,'horizontalalignment','center','fontsize',fs);
  75. set(gcf,'nextplot',np);
  76. axes(haold); %#ok<MAXES>
  77. if nargout,
  78. hout=ht;
  79. end