Matlab Subplot 居中标题 subtitle

最新版(2014-11-11)参考Matlab 2014b 多图居中标题 subtitle

http://hi.baidu.com/curbzz/item/be521df0bda30ee61a111f3f

之前写过一个类似的,但并不是各种情况都适用,去年修改了一个自适应的,如下:


function ht = subtitle(text)
% SUBTITLE ht = subtitle(text)
% Input:
%     text  Title of the graph
% Output:
%     ht   Handle of this text
%
% Example:
%   for kk = 1:6
%       subplot(2,3,kk);
%       plot(magic(3));
%   end
%   ht = subtitle('title in the middle top')
%   See also SET, POSITION, SUBPLOT, SUBTIGHT
%
 
% Latest revised 2013-5-10
% change algorithm to adapted to traditional subplot
% and specified positions
% Last revised 2011-04-15


% h1 = get(gcf,'children');
% axis1 = get(h1(end),'Position');
% axis2 = get(h1(end-kn+1),'Position');
%
% axest = [axis1(1),axis1(2)+axis1(4),1-axis1(1)-(1-axis2(1)-axis2(3)),0.01];
% ht = axes('Position',axest);
% axis(ht,'off')
% title(ht,text)


    h1 = get(gcf,'children');
%     nf = numel(h1);
    ind1 = strcmp('on',get(h1,'visible'));
    ind2 = strcmp('on',get(h1,'handlevisibility'));
    
    ind = find((ind1+ind2)==2);
    for kf = 1:numel(ind)
        pos(kf,:) = get(h1(ind(kf)),'Position');
    end

    left = min(squeeze(pos(:,1)));
    top = max(squeeze(pos(:,2)+pos(:,4)));
    % bottom = min(squeeze(pos(:,2)));
    right = max(squeeze(pos(:,1)+pos(:,3)));
    axest = [left,top-0.01,right-left,0.01];
    ht = axes('Position',axest);
    axis(ht,'off')
    title(ht,text)

end

=================================================================

这一次不需要定义列数,可以自适应的调整位置

之前版本在这里http://hi.baidu.com/curbzz/item/4d32087cad55103f6f29f637#a29f81fa1625b010d6ff8cfe

你可能感兴趣的:(Matlab Subplot 居中标题 subtitle)