user2824393
user2824393

Reputation: 649

c++ get double array from Matlab engine

I have C++ code which calls MATLAB function using MATLAB engine.

The MATLAB function result is an array of 3 doubles.

How can I get that array back to C++ as a double array ?

Upvotes: 1

Views: 347

Answers (1)

herohuyongtao
herohuyongtao

Reputation: 50707

You can use:

// e.g. array_name=[1 2 3] in MATLAB
Engine * matlab;
...
mxArray * m = engGetVariable(matlab, "array_name");
double * ptr = (double *) mxGetData(m); // ptr is the double array you need

// you can skip the following if you don't use OpenCV 
Mat mat(3, 1, CV_64F); // CV_64F <=> double
memcpy(mat.ptr(), ptr, 3*sizeof(double));

Upvotes: 3

Related Questions