A System object™ is a specialized kind of 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 code run efficiently.
Define all one-time calculations in the
and cache the results in a private property. Use the
for repeated calculations.
If properties are accessed more than once in the
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
In this example,
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
Property default values 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
Instead, create a method handle in
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
on a character vector comparison and assign the method handle to the
Because there is a loop in
stepImpl, assign the
to a local method handle,
myFun, and then use
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
If the number of System object inputs does not
change, do not implement the
Also do not implement the
when you explicitly list the inputs in the
instead of using
varargin. The same caveats apply
if you set the return argument from an object property, that object
property must have the
If the variables in a method do not need to retain their values between calls use local scope for those variables in that method.
For properties that do not change, define them in
have slower access times than
instead of the
public attribute for a property,
whenever possible. Some
public properties have
slower access times than
Avoid using a customized
set methods, whenever possible.
Avoid using character vector comparisons within a
whenever possible. Use
setupImpl for character vector
Specify Boolean values using
For best practices for including System objects in code generation, see System Objects in MATLAB Code Generation (MATLAB Coder).
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 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.