generate c classes for matlab classes -凯发k8网页登录

main content

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 ' and cpppreserveclasses to false.

  • 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.

rulecode snippet

the class constructor in matlab is mapped onto an init method. when an instance of the class is created, the generated code explicitly calls the init method.

the file myclass.cpp contains the definition of init.

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 mymethod that uses a private property prop of the object is called by an entry-point function. if mymethod is inlined in the generated code, the property prop must be visible from outside the object and changed to a public property.

to limit this occurrence, the code generator uses a special inlinig rule for public methods in this situation:

  • if the code configuration property inlinebetweenuserfunctions or the equivalent code generation setting inline between user functions in the matlab coder app is set to 'readability', the code generator does not inline the public method calls that appear outside the class definition.

in these situations, the same inlining rules apply to both ordinary functions and public methods:

  • the body of the function or the method contains an explicit coder.inline('always') or coder.inline('never') directive. this directive gets the highest precedence.

  • you set the code configuration property inlinebetweenuserfunctions or the equivalent code generation setting inline between user functions in the matlab coder app to 'never', 'speed', or 'always'.

  • a call to a method appears inside another method of the same class.

see .

the definition of the generated c class myclass is:

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 privatemethod does not appear in this definition. privatemethod is inlined in the definition of publicmethod (see in the file myclass.cpp) :

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 doublethisvalue has this signature:

static double doublethisvalue(double val);

methods that do not mutate the object are marked with the const qualifier in the generated code.

the public method calculatesomevalue does not mutate the object. the generated method has this signature:

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 file myclass.h. the implementations of the methods of the class are contained in the file myclass.cpp.

  • in the generated code, class hierarchies are flattened. for example, suppose that in your matlab code, class b inherits from class a. in the generated c code, classes b and a have no inheritance relationship between them. in the generated code, all properties and methods of class a are reproduced in the definition of class b.

  • 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 and setaccess attributes, the corresponding member of the generated class has the more permissive of the two attributes. for example, if a property prop 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

| |

related topics

    网站地图