Reputation: 8594
I have a C++ DLL which I call from Matlab code using calllib
.
I have no trouble calling a C++ function which has input parameters only or a function which returns mxArray
.
Now I have trouble calling a function which has several output parameters. Let's say, I need a C++ equivalent of this Matlab function which returns a matrix and an integer.
function [matrix, status] = foo()
status = 42;
matrix = ones(3,2);
end
Whatever I tried, it makes Matlab crash, for example:
DLL_API void foo(mxArray* iop_matrix, int* op_status)
{
mxSetM(iop_matrix, 3);
mxSetN(iop_matrix, 2);
*op_status = 42;
}
However I could easily get it to work when I need only one output parameter
DLL_API mxArray* foo(void)
{
return mxCreateNumericMatrix(3, 2, mxDOUBLE_CLASS, mxREAL);
}
What is the correct implementation of such function in C++?
Upvotes: 2
Views: 1016
Reputation: 8650
The Matlab function's 1st output parameter should be declared as the C function's output parameter. The Matlab function's 2nd output parameter should be declared as the C function's 1st input parameter which has mxArray **
type and so on.
A C++ function with several output parameters should look like
mxArray *foo(mxArray **matrix);
If the function contains input parameters in this case the function is declared as
mxArray *foo(mxArray **matrix, mxArray *_1stInpParam, mxArray *_2ndInpParam);
Upvotes: 3