ファイル名:Legend0.m

function [legh0,labelh0] = legend0(varargin)
% LEGEND0 Graph legend with uicontrol objects.
%   LEGEND0(string1,string2,string3, ...) puts a legend on the current plot using the specified strings as labels.
%   LEGEND0(H,string1,string2,string3, ...) puts a legend on the plot containing the handles in the vector H using
%       the specified strings as labels for the corresponding handles.
%   LEGEND0(AX,...) puts a legend on the axes with handle AX.
%   LEGEND0(M), where M is a string matrix, and LEGEND0(H,M) where H is a vector of handles to lines and patches
%       also works.
%   LEGEND0 OFF removes the legend from the current axes.
%   LEGEND0 with no arguments refreshes the current legend, if there is one.  If there are multiple legends present,
%       LEGEND0(legendhandle) refreshes the specified legend.
%   LEGEND0(...,Pos) places the legend in the specified
%   location:
%       0 = Automatic "best" placement (least conflict with data)
%       1 = Upper right-hand corner (default)
%       2 = Upper left-hand corner
%       3 = Lower left-hand corner
%       4 = Lower right-hand corner
%      -1 = To the right of the plot
%   To move the legend, press the left mouse button on the legend and drag to the desired location.
%   [legh,objh] = LEGEND0(...) returns a handle legh to the legend axes and an nx2 matrix objh containing the
%       line/patch handles and corresponding text handles in the legend axes.
%   LEGEND0 works on line graphs, bar graphs, pie graphs, ribbon plots, etc.  You can label any solid-colored patch
%       or surface object.
%   Examples:
%       x = 0:.2:12;
%       plot(x,bessel(1,x),x,bessel(2,x),x,bessel(3,x));
%       legend('First','Second','Third');
%       legend('First','Second','Third',-1)
%       b = bar(rand(10,5),'stacked'); colormap(summer);
%       hold on
%       x = plot(1:10,5*rand(10,1),'marker','square','markersize',12,'markeredgecolor','y',...
%           'markerfacecolor',[.6 0 .6],'linestyle','-','color','r','linewidth',2);
%       legend([b,x],'Carrots','Peas','Peppers','Green Beans','Cucumbers','Eggplant')
%
%   See also PLOT.

%   D. Thomas 5/6/93
%             9/6/95
%   Rich Radke 6/17/96 Latest Update
%   Copyright (c) 1984-97 by The MathWorks, Inc.
%   $Revision: 5.18 $  $Date: 1997/04/08 06:08:26 $

%   Obsolete syntax:
%
%   LEGEND0(linetype1,string1,linetype2,string2, ...) specifies the line types/colors for each label.
%   Linetypes can be any valid PLOT linetype specifying color, marker type, and linestyle, such as 'g:o'.
%
%   See HELP LSCAN for info on how the automatic placement of the legend works.

% legendコマンドで使用される文字サイズはAxesのFontSizeを採用しているので、この部分を変更してlegendコマンドを実行した
% 後に、再びAxesのFontSizeプロパティを元に戻す。

INITFONTSIZE  = get(gca,'FontSize');
if isempty(varargin)                        % legend0のみの時、legend部分の再描画を行うが、この時Axes軸の文字サイズを変えておかないといけない
    LEGOBJ = findobj(gcf,'Tag','legend');   % legendが同一Figure内に複数ある場合どのlegendがgcaのlegendかを見つけておく必要があり、又legendはaxes propertyを持つ
    if isempty(LEGOBJ)
        error('No Legend in this figure.');
    elseif length(LEGOBJ) >= 2
        for  k = 1:length(LEGOBJ)
            LEGUSER = get(LEGOBJ(k,1),'UserData');
            if LEGUSER.PlotHandle == gca
                LEGOBJ(1,1) = LEGOBJ(k,1);  % 該当するlegent objectをLEGOBJ(1,1)に代入しておく
                break;
            end
        end
    end
    TEXTOBJ = findobj(LEGOBJ(1,1),'Type','text'); % legend objectのchildrenにはline*4,text*1が入っている
    set(TEXTOBJ(1,1),'FontUnits','points');
    FONTSIZE = get(TEXTOBJ(1,1),'FontSize');
    set(gca,'FontSize',FONTSIZE);           % Legend上の文字サイズにAxesの文字サイズを合わせてから再描画させる
    if nargout > 1
        [legh0,labelh0] = legend;
    else
        legend;
    end
    set(gca,'FontSize',INITFONTSIZE);
elseif nargin==1 & strcmpi(varargin{1,1},'off') % legend0 off の時
    legend(varargin{1,:});
else                                        % その他(legend('aa','bb')等の時)
    while 1
        fprintf('Legend中の文字サイズを現在のデフォルト値の[%2.2f]から変更する。\n',INITFONTSIZE);
        FONTSIZE = input('フォントサイズを入力(単位ポイント)      => ');
        set(gca,'FontSize',FONTSIZE);
        if nargout > 1
            [legh0,labelh0] = legend(varargin{1,:});
        else
            legend(varargin{1,:});
        end
        set(gca,'FontSize',INITFONTSIZE);
        LINEAXES = findall(gca,'Type','line');  % ここからLine Property
        if ~isempty(LINEAXES)
            LINEWIDTH = input('ラインの太さ(変更無しならリターンのみ)  => ');
            if ~isempty(LINEWIDTH)
                set(LINEAXES,'LineWidth',LINEWIDTH);
            end
            MARKSIZE = input('マーカーサイズ(変更無しならリターンのみ)=> ');
            if ~isempty(MARKSIZE)
                set(LINEAXES,'MarkerSize',MARKSIZE);
            end
        end
        RETRY = input('もう一度やる?     y)YES n)NO           => ','s');
        if strcmpi(RETRY,'n'),      break;      end
    end
end