A System object™ is a specialized MATLAB® object that is optimized for iterative processing. Use System objects when you need to run an object multiple times or process data in a loop. When defining your own System object, use the following suggestions to help your System object run more quickly.
Define all one-time calculations in the
setupImpl method and cache the results in a private
property. Use the
stepImpl method for repeated calculations.
Specify Boolean values using
false instead of
If the variables in a method do not need to retain their values between calls, use local scope for those variables in that method.
Some methods use the
stepImpl algorithm inputs as their inputs, such as
processInputSizeChangeImpl. The inputs must match
the order of inputs to
stepImpl, but do not need to match the number of inputs. If your
implementation does not require any of the inputs to the System object, you can leave them all off.
getNumOutputsImpl methods, if you set the return
argument from an object property, that object property must have the
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. The code inserted by the MATLAB Editor menu uses
In many examples, 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.
For properties that do not change, define them in as
Tunable properties have slower access times than
Whenever possible, use the
private attribute instead of the
public attribute for a property. Some
public properties have slower access times
If properties are accessed more than once in the
stepImpl method, cache those properties as local
variables inside the method. A typical example of multiple property access is a loop. Iterative calculations using
cached local variables run faster than calculations that must access the properties of an object. When the
calculations for the method complete, you can save the local cached results back to the properties of that System
object. Copy frequently used tunable properties into private properties. This best practice also applies to the
For example, in this code
k is accessed multiple times in each loop iteration, but is saved to
the object property only once.
function y = stepImpl(obj,x) k = obj.MyProp; for p=1:100 y = k * x; k = k + 0.1; end obj.MyProp = k; end
Default values of properties are shared across all instances of an object. Two instances of a class can access the same default value if that property has not been overwritten by either instance.
Do not use character vector comparisons or character vector-based switch statements in the
method. Instead, create a method handle in
setupImpl. This handle points to a method in the same class
definition file. Use that handle in a loop in
This example shows how to use method handles and cached local variables in a loop to implement an efficient object. In
myMethod2 based on a character
vector comparison and assign the method handle to the
pMethodHandle property. Because there is a loop
stepImpl, assign the
pMethodHandle property to a local method handle,
myFun, and then use
myFun inside the loop.
classdef MyClass < matlab.System function setupImpl(obj) if strcmp(obj.Method, 'Method1') obj.pMethodHandle = @myMethod1; else obj.pMethodHandle = @myMethod2; end end function y = stepImpl(obj,x) myFun = obj.pMethodHandle; for p=1:1000 y = myFun(obj,x) end end end function y = myMethod1(x) y = x+1; end function y = myMethod2(x) y = x-1; end end
For System objects being included in Simulink, add the
StrictDefaults attribute. This attribute
sets all the
MutableImpl methods to return false by default.
For information about System objects and code generation, see System Objects in MATLAB Code Generation (MATLAB Coder).