matlab.System - Base class for System objects - MATLAB (original) (raw)

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

Input Specification

Output Specification

State Specification

Sample Time Specification

Interface Specification

Dialog and Appearance

Simulation Methods

Clone, Save and Load

Help

Functions for Implementing Authoring Methods

Methods for Implementing System Objects

nargin Number of input arguments for System object
nargout Number of output arguments for System object
getNumInputs Number of inputs required to call the System object
getNumOutputs Number of outputs from calling the System object
setup One-time set up tasks for System objects
reset Reset internal states of System object
step Run System object algorithm
release Release resources and allow changes to System object property values and input characteristics
clone Create duplicate System object
isDone End-of-data status
isLocked Determine if System object is in use

Creation

Description

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

example

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)

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);

Limitations

More About

expand all

You can apply attributes to the System object class and properties. To learn more about attributes, see Class Attributes orProperty Attributes.

Class Attribute

This table shows attributes that you can apply to the MATLAB® System object class.

Attribute Name Description
StrictDefaults Control the defaults for the methods that restrict specification modifications changes:isInputSizeMutableImplisInputComplexityMutableImplisInputDataTypeMutableImplisTunablePropertyDataTypeMutableImplisDiscreteStateSpecificationMutableImplBy default, these methods returntrue. When you add this class attribute, these methods return false by default. By making these methods return false, the specified aspects of the inputs, tunable properties, or discrete states cannot change. You can always implement these methods individually. Customized methods take precedence over the StrictDefaults attribute.For System objects used in Simulink®, this attribute only restricts input size changes because Simulink already restricts complexity and data type for tunable properties, inputs, and states.

Specify the class attribute value in parentheses followed by the class name, for example:

classdef (StrictDefaults) MySystemObject < matlab.System

Property Attributes

You can apply the following attributes to any property of a custom System object.

Nontunable Use Nontunable to prevent changes to a property value while the object is in use. By default, all properties are tunable. The Nontunable attribute is useful to lock down a property that has side effects when changed. This attribute is also useful for locking a property value assumed to be constant during processing. You should always specify properties that affect the number of input or output ports as Nontunable.
DiscreteState Use DiscreteState to mark a property so it will display its state value when you use the getDiscreteState method.

Version History

Introduced in R2011b