Main Content

プログラムによる System ブロック ダイアログ ボックスのカスタマイズ

対応する System object™ にプロパティやメソッドを追加して、MATLAB System ブロックのダイアログ ボックスをカスタマイズできます。タブを追加し、プロパティをグループやセクションにまとめ、ブロックの説明を追加し、シミュレーション タイプを制御し、カスタム ボタンを追加できます。

メモ

R2022b 以降では、マスク エディターを使用して MATLAB System ダイアログ ボックスを設計します。既存のダイアログ ボックスのカスタマイズをマスク エディターに移行することもできます。この機能を使用すると、System object ファイルで getPropertyGroupsImpl メソッドを開発または維持する必要がなくなります。詳細については、Customize MATLAB System Icon and Dialog Box Using Mask Editorを参照してください。

ブロック ダイアログ タブ、セクション、およびプロパティの順序の定義

この例では、プロパティの表示名を指定して getPropertyGroupImpl メソッドを変更し、MultipleGroupsWithTabs MATLAB System ブロックのブロック ダイアログ ボックスをカスタマイズします。

プロパティ ラベルの変更

ダイアログ ボックスに表示されるプロパティ ラベルを変更するには、コメントとプロパティ名の間にスペースを入れずに、この形式 %PropertyName Block Dialog Label で各プロパティの前にコメントを追加します。たとえば、StartValue プロパティを [Start Value] として表示するには、次のように指定します。

%StartValue Start Value
StartValue = 0

この例の MultipleGroupsWithTabs System object は MATLAB System ブロック ダイアログに表示するために各プロパティにラベルを付け直します。

ダイアログ ボックスの整理

MutlitpleGroupsWithTabs System object クラスは getPropertyGroupsImpl メソッドを定義します。getPropertyGroupsImpl メソッド内で、この例は 2 つのタブ (セクション グループ) および 3 つのパラメーター グループ (セクション) を定義します。

classdef MultipleGroupsWithTabs < matlab.System
    % MultipleGroupsWithTabs Customize block dialog with multiple tabs and parameter groups.
    
    % Public, tunable properties
    properties
        %StartValue Start Value
        StartValue = 0
        
        %EndValue End Value
        EndValue = 10
        
        Threshold = 1
        
        %BlockLimit Limit
        BlockLimit = 55
    end
    % Public Nontunable 
    properties(Nontunable)
        %IC1 First initial condition
        IC1 = 0
        
        %IC2 Second initial condition
        IC2 = 10
        
        %IC3 Third initial condition
        IC3 = 100

        %UseThreshold Use threshold
        UseThreshold (1,1) logical = true
    end
    
    methods (Static, Access = protected)
        function groups = getPropertyGroupsImpl
            % Section to always display above any tabs.
            alwaysSection = matlab.system.display.Section(...
                'Title','','PropertyList',{'BlockLimit'});
           
            % Group with no sections
            initTab = matlab.system.display.SectionGroup(...
                'Title','Initial conditions', ...
                'PropertyList',{'IC1','IC2','IC3'});
            
            % Section for the value parameters
            valueSection = matlab.system.display.Section(...
                'Title','Value parameters',...
                'PropertyList',{'StartValue','EndValue'});
            
            % Section for the threshold parameters
            thresholdSection = matlab.system.display.Section(...
                'Title','Threshold parameters',...
                'PropertyList',{'Threshold','UseThreshold'});
            
            % Group with two sections: the valueSection and thresholdSection sections
            mainTab = matlab.system.display.SectionGroup(...
                'Title','Main', ...
                'Sections',[valueSection,thresholdSection]);
            
            % Return an array with the group-less section, the group with
            % two sections, and the group with no sections.
            groups = [alwaysSection,mainTab,initTab];
        end
    end
end

結果のダイアログ ボックス

load_system('ShowSystemBlockDialog')
open_system('ShowSystemBlockDialog/MATLAB System')

プロパティ セクションの定義

この例では、プロパティの表示名を指定して getPropertyGroupImpl メソッドを変更し、MATLAB System ブロックのブロック ダイアログ ボックスをカスタマイズします。このカスタマイズは System object AddPropertySections で例示されます。

プロパティ ラベルの変更

ダイアログ ボックスに表示されるプロパティ ラベルを変更するには、パーセント記号とプロパティ名の間にスペースを入れずに、この形式 %PropertyName Block Dialog Label で各プロパティの前にコメントを追加します。たとえば、UseAlpha プロパティを [Use alpha] として表示するには、次のように指定します。

%UseAlpha Use alpha
UseAlpha = 0

この例に含まれる AddPropertySections System object は MATLAB System ブロック ダイアログに表示するために各プロパティにラベルを付け直します。

ダイアログ ボックスの整理

ダイアログ ボックスでプロパティを整理するために、AddPropertySections System object クラスは getPropertyGroupsImpl メソッドを定義します。getPropertyGroupsImpl メソッド内で、この例はそれぞれ 2 つのプロパティをもつ 2 つのセクションを定義します。

classdef AddPropertySections < matlab.System
    % AddPropertySections Customized dialog with two parameter sections
    
    % Public, tunable properties
    properties
        
        %NumberOfShapes Number of shapes
        NumberOfShapes = 10
        
        Alpha = 0.75
    end
 
    % Public, nontunable properties
    properties(Nontunable)
        Coloring (1, 1) {mustBeMember(Coloring,["red","blue","green"])} = "red"

        %UseAlpha Use alpha
        UseAlpha (1,1) logical = false
    end
    
    methods (Static, Access = protected)
        function groups = getPropertyGroupsImpl           
            % Section for the value parameters
            valueSection = matlab.system.display.Section(...
                'Title','Shape parameters',...
                'PropertyList',{'NumberOfShapes','Coloring'});
            
            % Section for the threshold parameters
            shadingSection = matlab.system.display.Section(...
                'Title','Shading parameters',...
                'PropertyList',{'UseAlpha','Alpha'});
            
            % Return an array with the two sections.
            groups = [valueSection, shadingSection];
        end
    end
end

結果のダイアログ ボックス

load_system('CustomSystemBlockDialog')
open_system('CustomSystemBlockDialog/MATLAB System')

ヘッダーの説明の追加

System object に getHeaderImpl メソッドを追加して、MATLAB System ブロックにヘッダー パネルを追加します。

getHeaderImpl を使用して MyCounter System object のパネルのタイトルとテキストを指定します。getHeaderImpl を指定しない場合、ブロックにはパネルのタイトルまたはテキストは表示されません。

すべての Impl メソッドと同様に、getHeaderImpl メソッドのアクセスを protected に設定します。このメソッドは内部でのみ呼び出されるからです。

methods (Static, Access = protected)
   function header = getHeaderImpl
      header = matlab.system.display.Header('MyCounter',...
        'Title','My Enhanced Counter');
   end
end

 完全なクラス定義

MATLAB System ブロックでのシミュレーション タイプの制御

シミュレーション タイプと、[シミュレーション実行方法] パラメーターが Simulink® MATLAB System ブロックのダイアログ ボックスに表示されるかどうかを指定します。このシミュレーション オプションは 'Code generation''Interpreted mode' です。

クラス定義ファイルに getSimulateUsingImpl メソッドを含めない場合、System object では両方のシミュレーション モードが許可され、既定値は 'Code generation' になります。showSimulateUsingImpl メソッドを含めなかった場合、ブロックのダイアログ ボックスに [シミュレーション実行方法] パラメーターが表示されます。

getSimulateUsingImpl メソッドと showSimulateUsingImpl メソッドは static に設定しなければならず、これらのメソッドのアクセスは protected に設定しなければなりません。

getSimulateUsingImpl を使用して、System object でインタープリター型実行のみが許可されるように指定します。

methods(Static,Access = protected)
   function simMode = getSimulateUsingImpl
       simMode = 'Interpreted execution';
    end
end

 完全なクラス定義

[シミュレーション実行方法] パラメーターを含む、結果のダイアログ ボックスを以下に示します。

MATLAB System dialog box showing the Simulate using drop down set to Interpreted execution and greyed out so users cannot change the option.

MATLAB System ブロックへのカスタム ボタンの追加

MATLAB System ブロックのダイアログ ボックスにボタンを追加します。このボタンをクリックすると、ランプ関数をプロットする Figure が開きます。

matlab.system.display.Action を使用して、MATLAB System ブロック ダイアログ ボックスのボタンに関連付ける MATLAB® 関数またはコードを定義します。また、この例では、ボタンのオプションを設定する方法と、actionData オブジェクト入力を使用して Figure のハンドルを格納する方法についても説明します。今回のコード例では、ボタンが複数回クリックされるときに、クリックのたびに新しい Figure を開くのではなく、同じ Figure を使用します。

methods(Static,Access = protected)
  function group = getPropertyGroupsImpl
    group = matlab.system.display.Section(mfilename('class'));
    group.Actions = matlab.system.display.Action(@(actionData,obj)...
       visualize(obj,actionData),'Label','Visualize');
  end
end
    
methods
  function obj = ActionDemo(varargin)
    setProperties(obj,nargin,varargin{:});
  end
        
  function visualize(obj,actionData)
    f = actionData.UserData;
    if isempty(f) || ~ishandle(f)
      f = figure;
      actionData.UserData = f;
    else
      figure(f); % Make figure current
    end
        
    d = 1:obj.RampLimit;
    plot(d);
  end
end

 ダイアログ ボタンのクラス定義ファイルの作成

[可視化] ボタンを含む、結果のダイアログ ボックスを以下に示します。

Resulting MATLAB System block dialog box with the custom button labeled "Visualize".

参考

| | | | | |

関連するトピック