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.
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
AddOne in the first line of your file.
classdef AddOne < matlab.System
System objects are composed of a base class,
matlab.System and may include one or more mixin
classes. You specify the base class and mixin classes on the first line of your class definition file.
Save the file and name it
stepImpl method contains the algorithm to execute when you run your object. Define this method so
that it contains the actions you want the System
object to perform.
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
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, require 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
By default, the number of inputs and outputs are both one. Inputs and outputs can be added using Inputs/Outputs. You can also use a variable number of inputs or outputs, see Change the Number of Inputs.
Alternatively, if you create your System
object by editing a MAT-file, you can add the
stepImpl method using Insert Method > Implement algorithm.
Change the computation in the
stepImpl method to add
1 to the value of
methods (Access = protected) function y = stepImpl(~,u) y = u + 1; end
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
Remove unused methods that are included by default in the basic template.
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