suplabel.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. function [ax,h]=suplabel(text,whichLabel,supAxes)
  2. % PLaces text as a title, xlabel, or ylabel on a group of subplots.
  3. % Returns a handle to the label and a handle to the axis.
  4. % [ax,h]=suplabel(text,whichLabel,supAxes)
  5. % returns handles to both the axis and the label.
  6. % ax=suplabel(text,whichLabel,supAxes)
  7. % returns a handle to the axis only.
  8. % suplabel(text) with one input argument assumes whichLabel='x'
  9. %
  10. % whichLabel is any of 'x', 'y', 'yy', or 't', specifying whether the
  11. % text is to be the xlable, ylabel, right side y-label,
  12. % or title respectively.
  13. %
  14. % supAxes is an optional argument specifying the Position of the
  15. % "super" axes surrounding the subplots.
  16. % supAxes defaults to [.08 .08 .84 .84]
  17. % specify supAxes if labels get chopped or overlay subplots
  18. %
  19. % EXAMPLE:
  20. % subplot(2,2,1);ylabel('ylabel1');title('title1')
  21. % subplot(2,2,2);ylabel('ylabel2');title('title2')
  22. % subplot(2,2,3);ylabel('ylabel3');xlabel('xlabel3')
  23. % subplot(2,2,4);ylabel('ylabel4');xlabel('xlabel4')
  24. % [ax1,h1]=suplabel('super X label');
  25. % [ax2,h2]=suplabel('super Y label','y');
  26. % [ax3,h2]=suplabel('super Y label (right)','yy');
  27. % [ax4,h3]=suplabel('super Title' ,'t');
  28. % set(h3,'FontSize',30)
  29. %
  30. % SEE ALSO: text, title, xlabel, ylabel, zlabel, subplot,
  31. % suptitle (Matlab Central)
  32. % Author: Ben Barrowes <barrowes@alum.mit.edu>
  33. %modified 3/16/2010 by IJW to make axis behavior re "zoom" on exit same as
  34. %at beginning. Requires adding tag to the invisible axes
  35. %modified 8/8/2018 to allow cells as text for multiline capability
  36. currax=findobj(gcf,'type','axes','-not','tag','suplabel');
  37. if nargin < 3
  38. supAxes=[.08 .08 .84 .84];
  39. ah=findall(gcf,'type','axes');
  40. if ~isempty(ah)
  41. supAxes=[inf,inf,0,0];
  42. leftMin=inf; bottomMin=inf; leftMax=0; bottomMax=0;
  43. axBuf=.04;
  44. set(ah,'units','normalized')
  45. ah=findall(gcf,'type','axes');
  46. for ii=1:length(ah)
  47. if strcmp(get(ah(ii),'Visible'),'on')
  48. thisPos=get(ah(ii),'Position');
  49. leftMin=min(leftMin,thisPos(1));
  50. bottomMin=min(bottomMin,thisPos(2));
  51. leftMax=max(leftMax,thisPos(1)+thisPos(3));
  52. bottomMax=max(bottomMax,thisPos(2)+thisPos(4));
  53. end
  54. end
  55. supAxes=[leftMin-axBuf,bottomMin-axBuf,leftMax-leftMin+axBuf*2,bottomMax-bottomMin+axBuf*2];
  56. end
  57. end
  58. if nargin < 2, whichLabel = 'x'; end
  59. if nargin < 1, help(mfilename); return; end
  60. if (~isstr(text) & ~iscellstr(text)) | ~isstr(whichLabel)
  61. error('text and whichLabel must be strings')
  62. end
  63. whichLabel=lower(whichLabel);
  64. ax=axes('Units','Normal','Position',supAxes,'Visible','off','tag','suplabel');
  65. if strcmp('t',whichLabel)
  66. set(get(ax,'Title'),'Visible','on')
  67. title(text);
  68. elseif strcmp('x',whichLabel)
  69. set(get(ax,'XLabel'),'Visible','on')
  70. xlabel(text);
  71. elseif strcmp('y',whichLabel)
  72. set(get(ax,'YLabel'),'Visible','on')
  73. ylabel(text);
  74. elseif strcmp('yy',whichLabel)
  75. set(get(ax,'YLabel'),'Visible','on')
  76. ylabel(text);
  77. set(ax,'YAxisLocation','right')
  78. end
  79. %for k=1:length(currax), axes(currax(k));end % restore all other axes
  80. for k=1:length(currax), set(gcf,'CurrentAxes',currax(k));end % restore all other axes
  81. if (nargout < 2)
  82. return
  83. end
  84. if strcmp('t',whichLabel)
  85. h=get(ax,'Title');
  86. set(h,'VerticalAlignment','middle')
  87. elseif strcmp('x',whichLabel)
  88. h=get(ax,'XLabel');
  89. elseif strcmp('y',whichLabel) | strcmp('yy',whichLabel)
  90. h=get(ax,'YLabel');
  91. end
  92. %%%ah=findall(gcf,'type','axes');
  93. %%%'sssssssss',kb