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
the first line of your file.
classdef AddOne < matlab.System
Save the file and name it
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.
In the basic System object you created, inspect
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
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,
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
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 y function to add 1
to the value of
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.
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