Reputation: 1
I have a problem with MEX files in C/C++ coding. I need to return a double complex array to Matlab but I am not able to do that and I don't find information about it. I show my code with some tries:
double complex output[nSymb];
nlhs = 1;
plhs[0] = mxCreateDoubleMatrix(nSymb,(mwSize)nlhs,mxCOMPLEX);
// 1º option
plhs[0] = output;
// 2º option
memcpy(plhs, output, nSymb * sizeof(double complex));
// 3º option
plhs = output;
Thanks you in advance.
Upvotes: 0
Views: 732
Reputation: 2438
You mentioned C++, so I've shown an example copying a std::vector<double>
into an MxArray
object, same can be applied to C-style arrays:
mxArray* CreateDoubleArray(const std::vector<double>& d)
{
mxArray* m = mxCreateDoubleMatrix(d.size(), 1, mxComplexity::mxReal); // mxComplexity::mxCOMPLEX
double* pm = mxGetDoubles (m); // mxGetComplexDoubles
for (size_t i=0; i<d.size(); i++)
{
pm[i] = d[i];
}
return m;
}
// ...
// Return value for the Mex:
plhs[0] = CreateDoubleArray(stdVecObj);
You can change the value_type
of the vector to std::complex
, and apply the changes suggested in the comments to handle complex type instead of double. mxGetComplexDoubles
returns a MxDOUBLE_CLASS*
documented here (it's just a struct with a real and imaginary component of type mxDouble).
Upvotes: 1