My project is in C++. I want to use matlab optimization nonlinsq
through matlab engine by "eval". I want to pass a function I wrote in C++ in the format of
void func(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
to matlab as a function handle without compiling it to mex.
I tried following Passing C/C++ callbacks into the matlab engine for creating a mxArray and then pass it to matlab workspace:
mxArray *fh = mclCreateSimpleFunctionHandle(func);
engPutVariable(engine, "func", fh);
mxDestroyArray(fh);
but the program crashed on the first line with access violation. In the call stack the last call before the violation was "mclmcrrt8_5.dll!000000000031dacd() Unknown"
What is the problem?..
Asking the MATLAB run-time engine to interpret the C/C++ code is the wrong way to do it (and I'm sure is impossible at this moment). The post you're referring to assumes that the C/C++ code is compiled into a shared object or a dynamically linked library. The mex
function itself requires a supported compiler that can be invoked in order to create the .mex
file.
TLDR: MATLAB cannot interpret C/C++ code.