Main Content

Define System Object for Use in Simulink

Develop System Object for Use in MATLAB System Block

You can develop a System object™ for use in a System block and interactively preview the block dialog box. This feature requires Simulink®.

With the System Block editing options, the MATLAB® Editor inserts predefined code into the System object. This coding technique helps you create and modify your System object faster and increases accuracy by reducing typing errors.

Using these options, you can also:

  • View and interact with the block dialog box design as you define the System object.

  • Add dialog box customization methods. If the block dialog box is open when you make changes, the block dialog design preview updates the display on saving the file.

  • Add icon methods. However, these elements display only on the MATLAB System Block in Simulink, not in the Preview Block Dialog.

Define Block Dialog Box for Plot Ramp

  1. Create a System object using the menu option New > System Object > Simulink Extension.

  2. Name the System object PlotRamp and save the file. This name becomes the block dialog box title.

  3. Delete the comment at the beginning of the file and replace it with the block description.

    % Display a button to launch a plot figure.

    This comment becomes the block parameters dialog box description, under the block title.

  4. Select System Block > Preview Block Dialog. The block dialog box displays as you develop the System object.

  5. Add a ramp limit by selecting Insert Property > Numeric. Then change the property name and set the value to 10.

      properties (Nontunable)
        RampLimit = 10;
      end
    
  6. Locate the getPropertyGrouplsImpl method using the Analyze button.

          function group = getPropertyGroupsImpl
              % Define property section(s) for System block dialog
              group = matlab.system.display.Section(mfilename('class'));
          end
  7. Create the group for the Visualize action.

          function group = getPropertyGroupsImpl
              % Define property section(s) for System block dialog
              group = matlab.system.display.Section(mfilename('class'));
              group.Actions = matlab.system.display.Action(@(~,obj)...
                visualize(obj),'Label','Visualize');
          end
  8. Add a function that adds code to display the Visualize button on the dialog box.

      methods
        function visualize(obj)
          figure;
          d = 1:obj.RampLimit;
          plot(d);
        end
      end
    
  9. As you add elements to the System block definition, save your file. Observe the effects of your code additions to the System block definition.

    The System Block menu also displays checks next to the methods included in your file.

  10. Delete any unused methods in the template or modify the methods to further customize the System object and System block. The class definition file now has all the code necessary for the PlotRamp System object.

    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
      end
        
      methods
        function visualize(obj)
          figure;
          d = 1:obj.RampLimit;
          plot(d);
        end
      end
    end

After you complete your System block definition, save it, and then load it into a MATLAB System block in Simulink.

Related Topics