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.
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.
Initialize
all System
objects properties that
releaseImpl
uses before the end
of setupImpl
.
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.
The complexity 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 (Simulink) block.
Do not use the Save and Restore Simulation State as SimState option for any System object in a MATLAB Function (Simulink) 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.
For fixed-point inputs, 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. See Generate Code for Global Data.
Methods
Code generation support is available only for these System object methods:
get
isDone
(for sources only)
set
(for tunable properties)
For System objects that you define, code generation support is available only for these methods:
releaseImpl
— Code
is not generated automatically for this method. To release an object,
you must explicitly call the release
method in your
code.
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 Get Started with MATLAB Coder and MATLAB Classes for more information.
Note
Most, but not all, System objects support code generation. Refer to the particular object’s reference page for information.
Using the MATLAB Function (Simulink) 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 Implementing MATLAB Functions Using Blocks (Simulink).
Using the MATLAB System (Simulink) 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 (Simulink).
MATLAB Compiler™ software supports System objects for use inside MATLAB functions. The compiler product does not support System objects for use in MATLAB scripts.