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 Class
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
AddOnein the first line of your file.
classdef AddOne < matlab.System
System objects are composed of a base class,
matlab.Systemand 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
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
stepImplmethod access is always set to
protectedbecause 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 object handle.
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
stepImplmethod using Insert Method > Implement algorithm.
Change the computation in the
stepImplmethod to add
1to 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 unused variables.
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