Customize System Block Dialog Box

Add Header to MATLAB System Block

Add a header panel to a System object™–based block implemented using a MATLAB System block.

Define Header Title and Text

Use getHeaderImpl to specify a panel title and text for the MyCounter System object.

If you do not specify the getHeaderImpl, the block does not display any title or text for the panel.

You always set the getHeaderImpl method access to protected because it is an internal method that end users do not directly call or run.

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

Complete Class Definition File with Defined Header

 classdef MyCounter < matlab.System
  
   % MyCounter Count values
     
    properties
       Threshold = 1
    end
    properties (DiscreteState)
       Count
    end
 
    methods (Static, Access = protected)
      function header = getHeaderImpl
         header = matlab.system.display.Header('MyCounter',...
           'Title','My Enhanced Counter',...
           'Text', 'This counter is an enhanced version.');
      end
    end

    methods (Access = protected)
       function setupImpl(obj,u)
          obj.Count = 0;
       end
       function y = stepImpl(obj,u)
          if (u > obj.Threshold)
             obj.Count = obj.Count + 1;
          end
          y = obj.Count;
       end
       function resetImpl(obj)
          obj.Count = 0;
       end
    end
end

Add Data Types Tab to MATLAB System Block

Add a Data Types tab to the MATLAB System block dialog box. This tab includes fixed-point data type settings.

Display Data Types Tab

Use matlab.system.showFiSettingsImpl to display the Data Types tab in the MATLAB System block dialog box.

methods (Static, Access = protected)
   function showTab = showFiSettingsImpl
      showTab = true;
   end
end

Complete Class Definition File with Data Types Tab

Use showFiSettingsImpl to display the Data Types tab for a System object that adds an offset to a fixed-point input.

classdef FiTabAddOffset < matlab.System
% FiTabAddOffset Add an offset to input

   properties
      Offset = 1;
   end
   
   methods
      function obj = FiTabAddOffset(varargin)
         setProperties(obj,nargin,varargin{:});
      end
   end
    
   methods (Access = protected)
      function y = stepImpl(~,u)
         y = u + obj.Offset;
      end
   end
    
   methods(Static, Access=protected)
      function header = getHeaderImpl
         header = matlab.system.display.Header('Title',...
              'Add Offset','Text','Add an offset to the input');
      end
         
      function isVisible = showFiSettingsImpl
         isVisible = true; 
      end
   end
end

Dialog Box with the Data Types Tab

Add Property Groups to System Object and MATLAB System Block

Define property sections and section groups for System object display. The sections and section groups display as panels and tabs, respectively, in the MATLAB System block dialog box.

Define Section of Properties

Use matlab.system.display.Section and getPropertyGroupsImpl to define two property group sections by specifying their titles and property lists.

If you do not specify a property in getPropertyGroupsImpl, the block does not display that property.

 methods (Static, Access = protected)
    function groups = getPropertyGroupsImpl
       valueGroup = matlab.system.display.Section(...
            'Title','Value parameters',...
            'PropertyList',{'StartValue','EndValue'});
 
       thresholdGroup = matlab.system.display.Section(...
            'Title','Threshold parameters',...
            'PropertyList',{'Threshold','UseThreshold'});
       groups = [valueGroup,thresholdGroup];
    end
 end

Define Group of Sections

Use matlab.system.display.SectionGroup, matlab.system.display.Section, and getPropertyGroupsImpl to define two tabs, each containing specific properties.

methods (Static, Access = protected)
   function groups = getPropertyGroupsImpl
      upperGroup = matlab.system.display.Section(...
           'Title', 'Upper threshold', ...
           'PropertyList',{'UpperThreshold'});
      lowerGroup = matlab.system.display.Section(...
           'Title','Lower threshold', ...
           'PropertyList',{'UseLowerThreshold','LowerThreshold'});
           
      thresholdGroup = matlab.system.display.SectionGroup(...
           'Title', 'Parameters', ...
           'Sections',  [upperGroup,lowerGroup]);
           
      valuesGroup = matlab.system.display.SectionGroup(...
           'Title', 'Initial conditions', ...
           'PropertyList',  {'StartValue'});
          
      groups = [thresholdGroup, valuesGroup];
   end
end

Complete Class Definition File with Property Group and Separate Tab

classdef EnhancedCounter < matlab.System 
   % EnhancedCounter  Count values considering thresholds
    
   properties
      UpperThreshold = 1;
      LowerThreshold = 0;
   end
   
   properties (Nontunable)
      StartValue = 0; 
      % Count values less than lower threshold
      UseLowerThreshold (1,1) logical = true; 
   end
        
   properties (DiscreteState)
      Count;
   end
        
   methods (Static, Access = protected)
      function groups = getPropertyGroupsImpl
         upperGroup = matlab.system.display.Section(...
              'Title', 'Upper threshold', ...
              'PropertyList',{'UpperThreshold'});
         lowerGroup = matlab.system.display.Section(...
              'Title','Lower threshold', ...
              'PropertyList',{'UseLowerThreshold','LowerThreshold'});
           
         thresholdGroup = matlab.system.display.SectionGroup(...
              'Title', 'Parameters', ...
              'Sections',  [upperGroup,lowerGroup]);
           
         valuesGroup = matlab.system.display.SectionGroup(...
              'Title', 'Initial conditions', ...
              'PropertyList',  {'StartValue'});
           
         groups = [thresholdGroup, valuesGroup];
      end
   end
    
   methods (Access = protected)
      function setupImpl(obj)
         obj.Count = obj.StartValue;
      end
        
      function y = stepImpl(obj,u)
         if obj.UseLowerThreshold
            if (u > obj.UpperThreshold) || ...
                  (u <  obj.LowerThreshold)
               obj.Count = obj.Count + 1;
            end
         else
            if (u > obj.UpperThreshold)
               obj.Count = obj.Count + 1;
            end
         end
         y = obj.Count;
      end
      function resetImpl(obj)
         obj.Count = obj.StartValue;
      end
        
               
      function flag = isInactivePropertyImpl(obj, prop)
         flag = false;
         switch prop
            case 'LowerThreshold'
               flag = ~obj.UseLowerThreshold;
         end
      end
   end
end

Customized Dialog Box

Control Simulation Type in MATLAB System Block

Specify a simulation type and whether the Simulate using parameter appears on the Simulink® MATLAB System block dialog box. The simulation options are 'Code generation' and 'Interpreted mode'.

If you do not include the getSimulateUsingImpl method in your class definition file, the System object allows both simulation modes and defaults to 'Code generation'. If you do not include the showSimulateUsingImpl method, the Simulate using parameter appears on the block dialog box.

You must set the getSimulateUsingImpl and showSimulateUsingImpl methods to static and the access for these methods to protected.

Use getSimulateUsingImpl to specify that only interpreted execution is allowed for the System object.

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

View the method in the complete class definition file.

classdef PlotRamp < matlab.System
  % Display a button to launch a plot figure.
        
  properties (Nontunable)
    RampLimit = 10;
  end
    
  methods(Static, Access=protected)
    function group = getPropertyGroupsImpl
      group = matlab.system.display.Section(mfilename('class'));
      group.Actions = matlab.system.display.Action(@(~,obj)...
            visualize(obj),'Label','Visualize');
    end

    function simMode = getSimulateUsingImpl
      simMode = 'Interpreted execution';
    end
  end
    
  methods
    function obj = ActionDemo(varargin)
      setProperties(obj,nargin,varargin{:});
    end
        
    function visualize(obj)
      figure;
      d = 1:obj.RampLimit;
      plot(d);
    end
   methods(Static,Access = protected)
   end
  end
end

Dialog Box with Simulation Type Parameter

Add Button to MATLAB System Block

Add a button to the MATLAB System block dialog box. This button opens a figure that plots a ramp function.

Define Action for Dialog Button

Use matlab.system.display.Action to define the MATLAB® function or code associated with a button in the MATLAB System block dialog box. The example also shows how to set button options and use an actionData object input to store a figure handle. This part of the code example uses the same figure when the button is clicked multiple times, rather than opening a new figure for each button click.

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

Complete Class Definition File for Dialog Button

Define a property group and a second tab in the class definition file.

classdef PlotRamp < matlab.System
  % Display a button to launch a plot figure.
        
  properties (Nontunable)
    RampLimit = 10;
  end
    
  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
end

Dialog Box with Visualize Button

See Also

| | | | | |

Related Topics