This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Best Practices for Defining System Objects

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 setupImpl method and cache the results in a private property. Use the stepImpl method for repeated calculations.

  • 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 updateImpl and outputImpl methods.

    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; 
      obj.MyProp = k; 

  • 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 stepImpl 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 stepImpl.

    This example shows how to use method handles and cached local variables in a loop to implement an efficient object. In setupImpl, choose myMethod1 or myMethod2 based on a character vector comparison and assign the method handle to the pMethodHandle property. Because there is a loop in 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;
          obj.pMethodHandle = @myMethod2;
      function y = stepImpl(obj,x)
        myFun = obj.pMethodHandle;
          for p=1:1000
            y = myFun(obj,x)
      function y = myMethod1(x)
        y = x+1;
      function y = myMethod2(x)
        y = x-1;

  • If the number of System object inputs does not change, do not implement the getNumInputsImpl method. Also do not implement the getNumInputsImpl method when you explicitly list the inputs in the stepImpl method instead of using varargin. The same caveats apply to the getNumOutputsImpl and varargout outputs.

  • For the getNumInputsImpl and getNumOutputsImpl methods, if you set the return argument from an object property, that object property must have the Nontunable attribute.

  • 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 as Nontunable properties. Tunable properties have slower access times than Nontunable properties

  • Use the protected or private attribute instead of the public attribute for a property, whenever possible. Some public properties have slower access times than protected and private properties.

  • Avoid using a customized stepImpl, get, or set methods, whenever possible.

  • Avoid using character vector comparisons within a customized stepImpl, get, or set methods, whenever possible. Use setupImpl for character vector comparisons instead.

  • Specify Boolean values using true or false instead of 1 or 0, respectively.

  • For best practices for including System objects in code generation, see System Objects in MATLAB Code Generation (MATLAB Coder).

System Object Input Arguments and ~ in Code Examples

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.

Was this topic helpful?