3

I have Matlab code using OO syntax which I need to convert into C/C++ source code (not binary but source).

I have read about Level 2 S-Functions and using Simulink Coder but

  • writing TLC for “fully inlining” TLC (not desired to rewrite complete routines in TLC)

    or

  • using a "function based wrapper" in TLC (not desired to implement routines in a C-File)

do not meet my requirements .

Are there other options to generate C/C++ source from Matlab code?

ToBe
  • 921
  • 1
  • 9
  • 23
  • Nice question. Actually, something like this would be very useful for I project I have. – kist Dec 21 '12 at 11:10
  • What is your motivation for wanting a version of the code that compiles as "C/C++"? – Mankarse Dec 21 '12 at 11:11
  • Solving this would enable maintenance independant of matlab. Especially convenient if you design something that is to be maintained by someone other than yourself while you prefer to make matlab code. – Dennis Jaheruddin Dec 21 '12 at 11:22
  • @Mankarse 1st Add this source to existing c++ project. Complete Project runs on real time operation system. (Linking with OS Specifics f.e. entry point) 2nd It's Customer Code. I won't touch – ToBe Dec 21 '12 at 11:23
  • Possible duplicate of: http://stackoverflow.com/questions/7731671/converting-matlab-to-c – Dennis Jaheruddin Dec 21 '12 at 11:25

2 Answers2

3

It is possible to generate C Code from OO-written Matlab Scripts. But it is necessary to wrap functions around the objects. You need to have Matlab Coder.

Matlab distinguishes between Value Classes and Handle Classes (all classes deriving from handle).

1. Handle Classes

1.1. Example

classdef myHandleClass < handle
properties (Access=private)
    num1;
end
   methods (Access=public)
       function obj = myHandleClass (num1) 
       % constructor
           if(nargin > 0)
             obj.num1=num1;
           end
       end
       function prod = product(obj, factor)
          prod = obj.num1.*factor;
       end
       function quot = quotient(obj,divisor)
           quot = obj.num1/divisor;
       end
   end
end 

1.2. Function using Handle Class

The object of myHandleClass needs to be persistent. Also the isempty function is obligatory.

     %#codegen   
     function prod= use_product_HandleClass(a,b)
     assert(isa(a,'double'));
     assert(isa(b,'double'));

     persistent p;  
      if isempty(p)
         p = myHandleClass(a);
      end

     prod = p.product(b);
     end

1.3. Generated Code

This is the most interesting part of the generated "use_product_HandleClass.c"

typedef struct {
  real_T num1;
} myHandleClass;

#endif                                 /*typedef_myHandleClass*/

/* Named Constants */

/* Variable Declarations */

/* Variable Definitions */
static myHandleClass p;

/* Function Declarations */

/* Function Definitions */
real_T use_product_HandleClass(real_T a, real_T b)
{
  if (!p_not_empty) {
    p.num1 = a;
    p_not_empty = TRUE;
  }

  return p.num1 * b;
}

2. Value Classes

2.1. Example of Value Class

   classdef myValueClass 
       properties (Access=private)
            num1;
       end
       methods (Access=public)
           function obj = myValueClass (num1) 
               if(nargin > 0)
                 obj.num1=num1;
               end
           end
           function prod = product(obj, factor)
              prod = obj.num1.*factor;     
           end
           function quot = quotient(obj,divisor)
               quot = obj.num1./divisor;
           end
       end
   end  

2.2. Function using Value Class

%#codegen
function res= use_product_ValueClass(a,b)
  assert(isa(a, 'double'))
  assert(isa(b, 'double'))
  p = myValueClass(a);
  res = p.product(b);
end

2.3. Generated Code

This is the most interesting part of the generated "use_product_ValueClass.c"

/* Function Definitions */
real_T use_product_ValueClass(real_T a, real_T b)
{
  return a * b;
}

3. References

„Generate Code for MATLAB Value Classes“.
http://www.mathworks.de/de/help/coder/ug/how-to-generate-code-for-matlab-value-classes.html.

„Generate Code for MATLAB Handle Classes and System Objects“.

http://www.mathworks.de/de/help/coder/ug/how-to-generate-code-for-matlab-handle-classes.html.

ToBe
  • 921
  • 1
  • 9
  • 23
2

Its not free, but Mathworks makes a product called Matlab Coder for generating C++ code from Matlab code.

Clark
  • 890
  • 8
  • 20