generate c classes for matlab classes
when you generate c code, the default behavior of the code generator produces c classes for the classes in your matlab® code. these include all matlab classes such as value classes, handle classes, and system objects.
you can change the default behavior of the code generator to produce structures for matlab classes. to change the default behavior, do one of the following:
in a code configuration object, set
targetlang
to'c '
andcpppreserveclasses
tofalse
.in the matlab coder™ app, in the generate step, set language to c . in the project build settings, on the code appearance tab, clear the generate c classes from matlab classes check box.
these examples illustrate certain rules that the code generator follows when mapping matlab classes to c classes.
example: generate code for a handle class that has private and public members
define a matlab handle class
myclass
:
classdef myclass < handle properties publicprop = 1; end properties(access = private) privateprop end methods function obj = myclass(value) obj.privateprop = value; end function publicmethod(obj,value) obj.privatemethod(value); end function res = calculatesomevalue(obj) res = obj.publicprop*obj.privateprop; end end methods (access = private) function privatemethod(obj,value) obj.publicprop = obj.publicprop value; obj.privateprop = obj.privateprop obj.doublethisvalue(value); end end methods(static) function res = doublethisvalue(val) res = 2 * val; end end end
define a matlab function foo
that uses
myclass
:
function out = foo(x,y) obj = myclass(x); obj.publicmethod(y); out = obj.calculatesomevalue; end
generate a static c library for foo
. specify the input argument to
be a double scalar. set the code generation configuration property
inlinebetweenuserfunctions
to
'readability'
.
cfg = coder.config('lib'); cfg.targetlang = 'c '; cfg.inlinebetweenuserfunctions = 'readability'; codegen -config cfg foo -args {0,0} -report
code generation successful: view report
open the code generation report and inspect the generated code. the file
myclass.h
contains the definition of the generated c class
myclass
:
class myclass { public: myclass *init(double value); void publicmethod(double value); static double doublethisvalue(double val); double calculatesomevalue() const; double publicprop; private: double privateprop; };
this is the code generated for the function
foo
:
double foo(double x, double y) { myclass obj; obj.init(x); obj.publicmethod(y); return obj.calculatesomevalue(); }
this table lists some of the rules that the code generator follows when generating c
classes and the corresponding snippets from the code generated for
myclass
.
rule | code snippet |
---|---|
the class constructor in matlab is mapped onto an | the file myclass *myclass::init(double value) { myclass *obj; obj = this; obj->publicprop = 1.0; obj->privateprop = value; return obj; } |
in most cases, if a class member is set as private in matlab, it is also set as private in the generated c code. in certain situations, inlining a public method in the
generated c code changes a private property in the your matlab code to a public property in the generated code and breaks data
encapsulation. for example, suppose that a public method
to limit this occurrence, the code generator uses a special inlinig rule for public methods in this situation:
in these situations, the same inlining rules apply to both ordinary functions and public methods:
see . | the definition of the generated c class class myclass { public: myclass *init(double value); void publicmethod(double value); static double doublethisvalue(double val); double calculatesomevalue() const; double publicprop; private: double privateprop; }; the visibility of all data and member functions is preserved between matlab and the generated code. the private method void myclass::publicmethod(double value) { this->publicprop = value; this->privateprop = myclass::doublethisvalue((value)); } |
static methods in matlab are mapped onto static c methods. | the generated code for the static method
static double doublethisvalue(double val); |
methods that do not mutate the object are marked with the
| the public method double calculatesomevalue() const; |
additional usage notes and limitations
these are some additional usage notes and limitations for generating c classes from matlab classes:
the class prototype for
myclass
is contained in the header filemyclass.h
. the implementations of the methods of the class are contained in the filemyclass.cpp
.in the generated code, class hierarchies are flattened. for example, suppose that in your matlab code, class
b
inherits from classa
. in the generated c code, classesb
anda
have no inheritance relationship between them. in the generated code, all properties and methods of classa
are reproduced in the definition of classb
.when a matlab class uses different types for its properties, the code generator produces a separate c class for each type usage.
if a matlab class member has different
getaccess
andsetaccess
attributes, the corresponding member of the generated class has the more permissive of the two attributes. for example, if a propertyprop
has the attributes(getaccess = public, setaccess = private)
,prop
is defined to be a public property in the generated code.while attempting to generate standalone code that contains c classes for matlab classes, you might get a warning message if both of these conditions are true:
you choose to generate reentrant code by enabling the
multiinstancecode
parameter in a code configuration object or by enabling the generate re-entrant code parameter in the matlab coder app.the destructor of a class in your matlab code has a persistent variable or calls another function that declares and uses a persistent variable.
in such situations, to generate code that contains c classes for matlab classes, disable the
multiinstancecode
or the generate re-entrant code parameter.
see also
| |