3

I have a handle class in Matlab that I want to be able to use in C++. I already learned here that I can't just generate a C++ class, but have to wrap my class with functions. The example in the other question only shows the use of one member function in a wrapper function. However, I need to be able to call several member functions of my class.

As I cannot pass my class instance to the wrapper functions as per the Matlab documentation, I don't see a way of having several functions operate on the same object.

Is it not possible to do this?

Any help is appreciated.

Community
  • 1
  • 1
Nicolas
  • 335
  • 3
  • 14
  • The answer that you've linked to is pretty old, written in March 2013. MATLAB Coder has changed a lot since then. – Sam Roberts May 18 '15 at 09:47
  • That's true. But according to the Matlab documentation, the same limitations still seem to exist. – Nicolas May 18 '15 at 10:36

2 Answers2

1

You cannot have classes as input and output for the main function for which you generate code. But you can have any number of sub-functions called from your main function which can take the object as input. The object is typically created from your main function and passed to your sub-functions. You then generate code using codegen "main function name". The generated code contains all the sub-functions.

You also should use coder.inline('never') in your sub-functions so that they show up as separate functions in generated code.

Navan
  • 4,407
  • 1
  • 24
  • 26
0

I don't see a way of having several functions operate on the same object.

Why not? You can just use pointer as input parameter.

int main() {
  int myarr[5] = {1, 2, 3, 4, 5};
  double myval1, myval2;
  myval = myfun1(myarr, 100);  // myarr is unchanged
  myfun2(&myarr, 200);         // myarr now has new values
  return 0;
}

double myfun1(int *arr, int para1) {
// @TODO1
}

void myfun2(int *arr, int para2) {
// @TODO2: here you can change value of *arr which is returned back to the calling function
}

myarr can be changed to any class you want.

scmg
  • 1,904
  • 1
  • 15
  • 24
  • What I am trying to do is translate a class written in Matlab to C++ using Matlab Coder. This tool is not able to translate a Matlab class directly, but it can translate a function that manipulates a Matlab class. Thus, to export a Matlab class to C++, the suggested procedure is to write **Matlab functions** that use the Matlab class and then use Matlab Coder on that function, which gives me a C/C++ function. According to the Matlab doc, Coder can't handle functions that with Matlab classes as input/output arguments. It seems the Matlab class has to live exclusively inside the Matlab function. – Nicolas May 18 '15 at 12:08