Documentation

Define Basic System Objects

This example shows how to create a basic System object™ that increments a number by one. The class definition file used in the example contains the minimum elements required to define a System object.

Create System Object

You can create and edit a MAT-file or use the MATLAB® Editor to create your System object. This example describes how to use the New menu in the MATLAB Editor.

In MATLAB, on the Editor tab, select New > System Object > Basic. A simple System object template opens.

Subclass your object from matlab.System. Replace Untitled with AddOne in the first line of your file.

classdef AddOne < matlab.System

Save the file and name it AddOne.m.

Define Algorithm

The stepImpl method contains the algorithm to execute when you call the step method on your object. Define this method so that it contains the actions you want the System object to perform.

  1. In the basic System object you created, inspect the stepImpl method template.

    methods (Access = protected)
       function y = stepImpl(obj,u)
          % Implement algorithm. Calculate y as a function of input u and
          % discrete states.
          y = u;
       end
    end

    The stepImpl method access is always set to protected because it is an internal method that users do not directly call or run.

    All methods, except static methods, expect the System object handle as the first input argument. The default value, inserted by MATLAB Editor, is obj. You can use any name for your System object handle.

    By default, the number of inputs and outputs are both 1. Inputs and outputs can be added using Inputs/Outputs. If you use variable number of inputs or outputs, insert the appropriate getNumInputsImpl or getNumOutputsImpl method.

    Alternatively, if you create your System object by editing a MAT-file, you can add the stepImpl method using Insert Method > Implement algorithm.

  2. Change the computation in the y function to add 1 to the value of u.

    methods (Access = protected)
        
        function y = stepImpl(~,u)
          y = u + 1;
        end
    

      Note:   Instead of passing in the object handle, you can use the tilde (~) to indicate that the object handle is not used in the function. Using the tilde instead of an object handle prevents warnings about unused variables.

  3. Remove the additional, unused methods that are included by default in the basic template. Alternatively, you can modify these methods to add more System object actions and properties. You can also make no changes, and the System object still operates as intended.

The class definition file now has all the code necessary for this System object.

classdef AddOne < matlab.System
% ADDONE Compute an output value one greater than the input value
  
  % All methods occur inside a methods declaration.
  % The stepImpl method has protected access
  methods (Access = protected)
    
    function y = stepImpl(~,u)
      y = u + 1;
    end
  end
end

See Also

| | |

Related Examples

More About

Was this topic helpful?