This example shows how to create a basic System object™ that increments a number by one.
The class definition file contains the minimum elements required to define a System object.
|On this page…|
Create the Class Definition File
Create a MATLAB® file named AddOne.m to contain the definition of your System object.
Subclass your object from matlab.System. Insert this line as the first line of your file.
classdef AddOne < matlab.System
Add the stepImpl method, which contains the algorithm that runs when users call the step method on your object. You always set the stepImpl method access 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. You can use any name for your System object handle.
In this example, instead of passing in the object handle, ~ is used to indicate that the object handle is not used in the function. Using ~ instead of an object handle prevents warnings about unused variables from occurring.
By default, the number of inputs and outputs are both one. To change the number of inputs or outputs, use the getNumInputsImpl or getNumOutputsImpl method, respectively.
methods (Access=protected) function y = stepImpl(~, x) y = x + 1; end end
Note: Instead of manually creating your class definition file, you can use File > New > System Object to open a sample System object file in the editor. You then can edit that file, using it as guideline, to create your own System object.
Complete Class Definition File for Basic 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(~,x) y = x + 1; end end end