Nicolas
Nicolas

Reputation: 355

Convert Matlab Handle class to C++

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.

Upvotes: 3

Views: 1051

Answers (2)

Navan
Navan

Reputation: 4477

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.

Upvotes: 1

scmg
scmg

Reputation: 1894

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.

Upvotes: 0

Related Questions