Main Content

matlab.System class

Base class for System objects

Description

matlab.System is the base class for System objects. In your class definition file, you must subclass your object from this base class (or from another class that derives from this base class). Subclassing allows you to use the implementation methods and service methods provided by this base class to build your object. Type this syntax as the first line of your class definition file to directly inherit from the matlab.System base class, where ObjectName is the name of your object:

classdef ObjectName < matlab.System

Note

You must set Access = protected for each matlab.System method you use in your code.

The matlab.System class is a handle class.

Class Attributes

Abstract
true
HandleCompatible
true
StrictDefaults
false

For information on class attributes, see Class Attributes.

Methods

expand all

Creation

Description

example

function obj = ObjectName(varargin) constructs an ObjectName System object™ and sets properties from name-value pair inputs.

The System object constructor is a public method in the class file. The method name matches the class name. When you create a System object, the constructor is called to initialize properties to nondefault values. The constructor returns a new System object.

Inside the constructor, call setProperties using one of these syntaxes.

example

setProperties(obj, nargin, varargin{:}) specifies that properties of the System object are set using Name-Value arguments.

example

setProperties(obj, nargin, varargin{:}, Prop1, ..., PropN) specifies that properties of the System object are set using Value-only arguments

Tip: Within the body of the constructor, do not assign property values. This practice can cause problems if you use the System object in multiple environments ,such as in a System block, in a MATLAB script, and in generated code. Instead, use default property values or change values inside setupImpl.

Examples

collapse all

This example shows how to author a basic System object™ called AddOne.

In MATLAB®, select New > System object > Basic. A new editor window opens with default syntax and comments for a new System object.

Rename the class AddOne. Modify the default template so your class looks like this:

classdef AddOne < matlab.System
% ADDONE Compute an output value that increments the input by one

    methods (Access = protected)
       % Implement algorithm. Calculate y as a function of input x.
       function y = stepImpl(~,x)
          y = x + 1;
       end    
    end
end

Use this object by creating an instance of AddOne and running the object with input.

addingObject = AddOne;
x = 5;
addingObject(x)
ans = 6

Define a System object constructor that allows name-value pair input arguments.

Define a constructor for name-value pair inputs.

function obj = Counter(varargin)
    % Support name-value pair arguments when constructing object
    setProperties(obj,nargin,varargin{:})
end

With this constructor body, create a Counter object using name-value pairs.

myObj = Counter('StartValue',0,'UseIncrement',true);

Define a System object constructor with a value-only input property.

Define a constructor with 'StartValue' as a value-only property input. This constructor also allows name-value inputs.

function obj = Counter(varargin)
    % Support value-only argument for StartValue when instantiating
    setProperties(obj,nargin,varargin{:},'StartValue');
end

With this constructor body, create a Counter object using a value-only argument for StartValue and name-value pairs for other properties.

myObj = Counter(0,'UseIncrement',true);

More About

expand all

Version History

Introduced in R2011b