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 an option on the New > System Object menu to open a template. The Basic template opens a simple System object template. The Advanced template includes more advanced features of System objects, such as backup and restore. The Simulink Extension template includes additional customizations of the System object for use in the Simulink® MATLAB System block. You then can edit the template 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