Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

クラスのヘルプの作成

doc コマンドからのヘルプ テキスト

doc コマンドを使用してクラスのヘルプを表示する場合、MATLAB® はクラス定義から導き出した情報を自動的に表示します。

たとえば、以下のように、いくつかのプロパティとメソッドを含む someClass.m というクラス定義ファイルを作成します。

classdef someClass
    % someClass Summary of this class goes here
    %   Detailed explanation goes here
    
    properties
        One     % First public property
        Two     % Second public property
    end
    properties (Access=private)
        Three   % Do not show this property
    end
      
    methods
        function obj = someClass
            % Summary of constructor
        end
        function myMethod(obj)
            % Summary of myMethod
            disp(obj)
        end
    end
    methods (Static)
        function myStaticMethod
            % Summary of myStaticMethod
        end
    end
    
end

doc コマンドを使用してクラス定義からヘルプ テキストと詳細を表示します。

doc someClass

カスタム ヘルプ テキスト

doc コマンドと help コマンドの両方が表示に含めるクラスの情報を追加できます。doc コマンドは、生成された HTML ページの最上部、クラス定義から導き出した情報の上にヘルプ テキストを表示します。help コマンドは、ヘルプ テキストをコマンド ウィンドウに表示します。詳細については、以下を参照してください。

クラス

ファイルの classdef ステートメントの直後の行にコメントを入れて、クラスのヘルプ テキストを作成します。たとえば、下に示すようにファイル myClass.m を作成します。

classdef myClass
    % myClass   Summary of myClass
    % This is the first line of the description of myClass.
    % Descriptions can include multiple lines of text.
    %
    % myClass Properties:
    %    a - Description of a
    %    b - Description of b
    %
    % myClass Methods:
    %    doThis - Description of doThis
    %    doThat - Description of doThat

    properties
        a
        b
    end
    
    methods
        function obj = myClass
        end
        function doThis(obj)
        end
        function doThat(obj)
        end
    end
    
end

最初のコメント ブロックのプロパティとメソッドの一覧および説明はオプションです。クラス名に続けて Properties または Methods とコロン (:) から成るコメント行が含まれている場合、MATLAB はそのプロパティまたはメソッドのヘルプへのハイパーリンクを作成します。

help コマンドを使用してクラスのヘルプ テキストをコマンド ウィンドウに表示します。

help myClass
  myClass   Summary of myClass
  This is the first line of the description of myClass.
  Descriptions can include multiple lines of text.
 
  myClass Properties:
     a - Description of a
     b - Description of b

  myClass Methods:
     doThis - Description of doThis
     doThat - Description of doThat 

メソッド

関数定義ステートメントの直後にコメントを挿入して、メソッドのヘルプを作成します。たとえば、クラス定義ファイル myClass.m を変更して、doThis メソッドのヘルプと含めます。

       function doThis(obj)
        % doThis  Do this thing
        %   Here is some help text for the doThis method.
        %
        %   See also DOTHAT.
        
        disp(obj)
        end        

help コマンドを使用してメソッドのヘルプ テキストをコマンド ウィンドウに表示します。クラス名とメソッド名をドット (.) で区切って指定します。

help myClass.doThis
  doThis  Do this thing
    Here is some help text for the doThis method.

    See also doThat.

プロパティ

プロパティのヘルプを作成する方法は 2 とおりあります。

  • プロパティ定義の上にコメント行を挿入する。複数行のヘルプ テキストを作成するときはこの方法を使用します。

  • プロパティ定義の横に単一行のコメントを追加する。

定義の上にあるコメントは、定義の横にあるコメントより優先されます。

たとえば、クラス定義ファイル myClass.m でプロパティ定義を変更します。

    properties
        a          % First property of myClass

        % b - Second property of myClass
        % The description for b has several 
        % lines of text.
        b          % Other comment
    end

help コマンドを使用してプロパティのヘルプをコマンド ウィンドウに表示します。クラス名とプロパティ名をドット (.) で区切って指定します。

help myClass.a
 a -  First property of myClass
help myClass.b
  b - Second property of myClass
  The description for b has several 
  lines of text.

列挙値

プロパティと同様に、列挙値のヘルプを作成する方法は 2 とおりあります。

  • 列挙値定義の上にコメント行を挿入する。複数行のヘルプ テキストを作成するときはこの方法を使用します。

  • 列挙値定義の横に単一行のコメントを追加する。

定義の上にあるコメントは、定義の横にあるコメントより優先されます。

たとえば、myEnumeration.m というファイルに列挙クラスを作成します。

classdef myEnumeration
    enumeration
        uno,         % First enumeration

        % DOS - Second enumeration
        % The description for DOS has several 
        % lines of text.
        dos          % A comment (not help text)
    end
end

help コマンドを使用してヘルプをコマンド ウィンドウに表示します。クラス名と列挙メンバーをドット (.) で区切って指定します。

help myEnumeration.uno
uno -  First enumeration
help myEnumeration.dos
  dos - Second enumeration
  The description for dos has several 
  lines of text.

イベント

プロパティおよび列挙値と同様に、イベントのヘルプを作成する方法は 2 とおりあります。

  • イベント定義の上にコメント行を挿入する。複数行のヘルプ テキストを作成するときはこの方法を使用します。

  • イベント定義の横に単一行のコメントを追加する。

定義の上にあるコメントは、定義の横にあるコメントより優先されます。

たとえば、hasEvents.m というファイルにクラスを作成します。

classdef hasEvents < handle
   events
       Alpha     % First event

       % Beta - Second event
       % Additional text about second event.
       Beta      % (not help text)
   end
   
   methods
       function fireEventAlpha(h)
           notify(h,'Alpha')
       end
       
       function fireEventBeta(h)
           notify(h,'Beta')
       end
   end
end

help コマンドを使用してヘルプをコマンド ウィンドウに表示します。クラス名とイベントをドット (.) で区切って指定します。

help hasEvents.Alpha
 Alpha -  First event
help hasEvents.Beta
  Beta - Second event
  Additional text about second event.

参考

|

関連するトピック