mtit.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. %MTIT creates a major title in a figure with many axes
  2. %
  3. % MTIT
  4. % - creates a major title above all
  5. % axes in a figure
  6. % - preserves the stack order of
  7. % the axis handles
  8. %
  9. %SYNTAX
  10. %-------------------------------------------------------------------------------
  11. % P = MTIT(TXT,[OPT1,...,OPTn])
  12. % P = MTIT(FH,TXT,[OPT1,...,OPTn])
  13. %
  14. %INPUT
  15. %-------------------------------------------------------------------------------
  16. % FH : a valid figure handle [def: gcf]
  17. % TXT : title string
  18. %
  19. % OPT : argument
  20. % -------------------------------------------
  21. % xoff : +/- displacement along X axis
  22. % yoff : +/- displacement along Y axis
  23. % zoff : +/- displacement along Z axis
  24. %
  25. % title modifier pair(s)
  26. % -------------------------------------------
  27. % TPx : TVx
  28. % see: get(text) for possible
  29. % parameters/values
  30. %
  31. %OUTPUT
  32. %-------------------------------------------------------------------------------
  33. % par : parameter structure
  34. % .pos : position of surrounding axis
  35. % .oh : handle of last used axis
  36. % .ah : handle of invisible surrounding axis
  37. % .th : handle of main title
  38. %
  39. %EXAMPLE
  40. %-------------------------------------------------------------------------------
  41. % subplot(2,3,[1 3]); title('PLOT 1');
  42. % subplot(2,3,4); title('PLOT 2');
  43. % subplot(2,3,5); title('PLOT 3');
  44. % axes('units','inches',...
  45. % 'color',[0 1 .5],...
  46. % 'position',[.5 .5 2 2]); title('PLOT 41');
  47. % axes('units','inches',...
  48. % 'color',[0 .5 1],...
  49. % 'position',[3.5 .5 2 2]); title('PLOT 42');
  50. % shg;
  51. % p=mtit('the BIG title',...
  52. % 'fontsize',14,'color',[1 0 0],...
  53. % 'xoff',-.1,'yoff',.025);
  54. % % refine title using its handle <p.th>
  55. % set(p.th,'edgecolor',.5*[1 1 1]);
  56. % created:
  57. % us 24-Feb-2003 / R13
  58. % modified:
  59. % us 24-Feb-2003 / CSSM
  60. % us 06-Apr-2003 / TMW
  61. % us 13-Nov-2009 17:38:17
  62. %-------------------------------------------------------------------------------
  63. function par=mtit(varargin)
  64. defunit='normalized';
  65. if nargout
  66. par=[];
  67. end
  68. % check input
  69. if nargin < 1
  70. help(mfilename);
  71. return;
  72. end
  73. if isempty(get(0,'currentfigure'))
  74. disp('MTIT> no figure');
  75. return;
  76. end
  77. vl=true(size(varargin));
  78. if ischar(varargin{1})
  79. vl(1)=false;
  80. figh=gcf;
  81. txt=varargin{1};
  82. elseif any(ishandle(varargin{1}(:))) &&...
  83. ischar(varargin{2})
  84. vl(1:2)=false;
  85. figh=varargin{1};
  86. txt=varargin{2};
  87. else
  88. error('MTIT> invalid input');
  89. end
  90. vin=varargin(vl);
  91. [off,vout]=get_off(vin{:});
  92. % find surrounding box
  93. ah=findall(figh,'type','axes');
  94. if isempty(ah)
  95. disp('MTIT> no axis');
  96. return;
  97. end
  98. oah=ah(1);
  99. ou=get(ah,'units');
  100. set(ah,'units',defunit);
  101. ap=get(ah,'position');
  102. if iscell(ap)
  103. ap=cell2mat(get(ah,'position'));
  104. end
  105. ap=[ min(ap(:,1)),max(ap(:,1)+ap(:,3)),...
  106. min(ap(:,2)),max(ap(:,2)+ap(:,4))];
  107. ap=[ ap(1),ap(3),...
  108. ap(2)-ap(1),ap(4)-ap(3)];
  109. % create axis...
  110. xh=axes('position',ap);
  111. % ...and title
  112. th=title(txt,vout{:});
  113. tp=get(th,'position');
  114. set(th,'position',tp+off);
  115. set(xh,'visible','off','hittest','on');
  116. set(th,'visible','on');
  117. % reset original units
  118. ix=find(~strcmpi(ou,defunit));
  119. if ~isempty(ix)
  120. for i=ix(:).'
  121. set(ah(i),'units',ou{i});
  122. end
  123. end
  124. % ...and axis' order
  125. uistack(xh,'bottom');
  126. axes(oah); %#ok
  127. if nargout
  128. par.pos=ap;
  129. par.oh=oah;
  130. par.ah=xh;
  131. par.th=th;
  132. end
  133. end
  134. %-------------------------------------------------------------------------------
  135. function [off,vout]=get_off(varargin)
  136. % search for pairs <.off>/<value>
  137. off=zeros(1,3);
  138. io=0;
  139. for mode={'xoff','yoff','zoff'};
  140. ix=strcmpi(varargin,mode);
  141. if any(ix)
  142. io=io+1;
  143. yx=find(ix);
  144. ix(yx+1)=1;
  145. off(1,io)=varargin{yx(end)+1};
  146. varargin=varargin(xor(ix,1));
  147. end
  148. end
  149. vout=varargin;
  150. end
  151. %-------------------------------------------------------------------------------