To generate efficient standalone code for MATLAB® classes, you must use classes differently than when running your code in the MATLAB environment.
What's Different | More Information |
---|---|
Class must be in a single file. Because of this limitation,
code generation is not supported for a class definition that uses
an @ -folder. | |
Restricted set of language features. | Language Limitations |
Restricted set of code generation features. | Code Generation Features Not Compatible with Classes |
Definition of class properties. | Defining Class Properties for Code Generation |
Use of handle classes. | |
Calls to base class constructor. | Calls to Base Class Constructor |
Global variables containing MATLAB objects are not supported for code generation. | N/A |
Inheritance from built-in MATLAB classes is not supported. | Inheritance from Built-In MATLAB Classes Not Supported |
Although code generation support is provided for common features of classes such as properties and methods, there are a number of advanced features which are not supported, such as:
Events
Listeners
Arrays of objects
Recursive data structures
Linked lists
Trees
Graphs
Overloadable operators subsref
, subsassign
,
and subsindex
In MATLAB, classes can define their own versions of the subsref
, subsassign
,
and subsindex
methods. Code generation does not
support classes that have their own definitions of these methods.
The empty
method
In MATLAB, classes have a built-in static method, empty
,
which creates an empty array of the class. Code generation does not
support this method.
The following MATLAB handle class methods:
addlistener
delete
eq
findobj
findpro
The AbortSet
property attribute
You can generate code for entry-point MATLAB functions that use classes, but you cannot generate code directly for a MATLAB class.
For example, if ClassNameA
is a class definition,
you cannot generate code by executing:
codegen ClassNameA
If an entry-point MATLAB function has an input or output that is a MATLAB class, you cannot generate code for this function.
For example, if function foo
takes one input, a
,
that is a MATLAB object, you cannot generate code for foo
by
executing:
codegen foo -args {a}
Code generation does not support classes in matrices or structures. As a workaround, consider using cell arrays because code generation supports classes in cell arrays.
Code generation does not support assigning an object
of a value class into a nontunable property. For example, obj.prop=v;
is
invalid when prop
is a nontunable property and v
is
an object based on a value class.
You cannot use coder.extrinsic
to
declare a class or method as extrinsic.
You cannot pass a MATLAB class to the coder.ceval
function.
If you use classes in code in the MATLAB Function block, you cannot use the debugger to view class information.
The coder.nullcopy
function does
not support MATLAB classes as inputs.
For code generation, you must define class properties differently than you do when running your code in the MATLAB environment:
Code generation does not support the property restriction
syntax. For example, the following class definition is not allowed
because it uses the property restriction syntax to restrict the types
of the Number
and Today
properties.
classdef Myclass properties Number double Today char = date; end end
After defining a property, do not assign it an incompatible type. Do not use a property before attempting to grow it.
When you define class properties for code generation, consider the same factors that you take into account when defining variables. In the MATLAB language, variables can change their class, size, or complexity dynamically at run time so you can use the same variable to hold a value of varying class, size, or complexity. C and C++ use static typing. Before using variables, to determine their type, the code generator requires a complete assignment to each variable. Similarly, before using properties, you must explicitly define their class, size, and complexity.
Initial values:
If the property does not have an explicit initial value, the code generator assumes that it is undefined at the beginning of the constructor. The code generator does not assign an empty matrix as the default.
If the property does not have an initial value and the code generator cannot determine that the property is assigned prior to first use, the software generates a compilation error.
For System objects, if a nontunable property is a structure, you must completely assign the structure. You cannot do partial assignment using subscripting.
For example, for a nontunable property, you can use the following assignment:
mySystemObject.nonTunableProperty=struct('fieldA','a','fieldB','b');
You cannot use the following partial assignments:
mySystemObject.nonTunableProperty.fieldA = a; mySystemObject.nonTunableProperty.fieldB = b;
If dynamic memory allocation is enabled, code generation supports variable-size properties for handle classes. Without dynamic memory allocation, you cannot generate code for handle classes that have variable-size properties.
coder.varsize
is
not supported for class properties.
MATLAB computes class initial values at class
loading time before code generation. If you use persistent variables
in MATLAB class property initialization, the value of the persistent
variable computed when the class loads belongs to MATLAB; it
is not the value used at code generation time. If you use coder.target
in MATLAB class
property initialization, coder.target('MATLAB')
returns true
(1)
.
If a class constructor contains a call to the constructor of
the base class, the call to the base class constructor must come before for
, if
, return
, switch
or while
statements.
For example, if you define a class B
based
on class A
:
classdef B < A methods function obj = B(varargin) if nargin == 0 a = 1; b = 2; elseif nargin == 1 a = varargin{1}; b = 1; elseif nargin == 2 a = varargin{1}; b = varargin{2}; end obj = obj@A(a,b); end end end
Because the class definition for B
uses an if
statement
before calling the base class constructor for A
,
you cannot generate code for function callB
:
function [y1,y2] = callB x = B; y1 = x.p1; y2 = x.p2; end
However, you can generate code for callB
if
you define class B
as:
classdef B < A methods function obj = NewB(varargin) [a,b] = getaandb(varargin{:}); obj = obj@A(a,b); end end end function [a,b] = getaandb(varargin) if nargin == 0 a = 1; b = 2; elseif nargin == 1 a = varargin{1}; b = 1; elseif nargin == 2 a = varargin{1}; b = varargin{2}; end end
You cannot generate code for classes that inherit from built-in MATLAB classes. For example, you cannot generate code for the following class:
classdef myclass < double