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 call the step method 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.

  • For parameters that do not change, define them in a locked object as Nontunable properties.

  • 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 outputs, getNumOutputsImpl and varargout.

  • Variables that do not need to retain their values between calls should have local scope for that method.

  • If properties are accessed more than once in the stepImpl method, or in the updateImpl and outputImpl methods, cache those properties as local variables inside the method. 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.

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

Was this topic helpful?