Documentation

System Objects in MATLAB Code Generation

You can generate C/C++ code in MATLAB® from your system that contains System objects by using MATLAB Coder™. You can generate efficient and compact code for deployment in desktop and embedded systems and accelerate fixed-point algorithms.

Usage Rules and Limitations for System Objects for Generating Code

The following usage rules and limitations apply to using System objects in code generated from MATLAB.

Object Construction and Initialization

  • If objects are stored in persistent variables, initialize System objects once by embedding the object handles in an if statement with a call to isempty().

  • Set arguments to System object™ constructors as compile-time constants.

  • You cannot initialize System objects properties with other MATLAB class objects as default values in code generation. You must initialize these properties in the constructor.

Inputs and Outputs

  • System objects accept a maximum of 1024 inputs. A maximum of eight dimensions per input is supported.

  • The data type of the inputs should not change.

  • If you want the size of inputs to change, verify that support for variable-size is enabled. Code generation support for variable-size data also requires that variable-size support is enabled. By default in MATLAB, support for variable-size data is enabled.

  • System objects predefined in the software do not support variable-size if their data exceeds the DynamicMemoryAllocationThreshold value.

  • Do not set System objects to become outputs from the MATLAB Function block.

  • Do not use the Save and Restore Simulation State as SimState option for any System object in a MATLAB Function block.

  • Do not pass a System object as an example input argument to a function being compiled with codegen.

  • Do not pass a System object to functions declared as extrinsic (functions called in interpreted mode) using the coder.extrinsic function. System objects returned from extrinsic functions and scope System objects that automatically become extrinsic can be used as inputs to another extrinsic function. But, these functions do not generate code.

Properties

  • In MATLAB System blocks, you cannot use variable-size for discrete state properties of System objects. Private properties can be variable-size.

  • Objects cannot be used as default values for properties.

  • You can only assign values to nontunable properties once, including the assignment in the constructor.

  • Nontunable property values must be constant.

  • If a tunable property has dependent data type properties, you can set tunable properties only at construction time or after the object is locked.

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

Global Variables

  • Global variables are allowed in a System object, unless you are using that System object in Simulink® via the MATLAB System block. To avoid syncing global variables between a MEX file and the workspace, use a coder configuration object. For example:

    f = coder.MEXConfig; 
    f.GlobalSyncMethod = 'NoSync'
    Then, include '-config f' in your codegen command.

Methods

System Objects in codegen

You can include System objects in MATLAB code in the same way you include any other elements. You can then compile a MEX file from your MATLAB code by using the codegen command, which is available if you have a MATLAB Coder license. This compilation process, which involves a number of optimizations, is useful for accelerating simulations. See Getting Started with MATLAB Coder (MATLAB Coder) and MATLAB Classes (MATLAB Coder) for more information.

Note

Most, but not all, System objects support code generation. Refer to the particular object’s reference page for information.

System Objects in the MATLAB Function Block

Using the MATLAB Function block, you can include any System object and any MATLAB language function in a Simulink model. This model can then generate embeddable code. System objects provide higher-level algorithms for code generation than do most associated blocks. For more information, see What Is a MATLAB Function Block?.

System Objects in the MATLAB System Block

Using the MATLAB System block, you can include in a Simulink model individual System objects that you create with a class definition file. The model can then generate embeddable code. For more information, see MATLAB System Block.

System Objects and MATLAB Compiler Software

MATLAB Compiler™ software supports System objects for use inside MATLAB functions. The compiler product does not support System objects for use in MATLAB scripts.

Was this topic helpful?