0

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?..

Community
  • 1
  • 1
user2070148
  • 45
  • 11
  • Please let me know if I got you straight: you want the MATLAB engine to *interpret* your C++ source code? That's not going to happen. –  Jan 05 '16 at 10:13
  • I think you got it right. But I found some links as the one I posted to some solutions I couldn't make work. You say it can only get a sort of dll? Are you sure? – user2070148 Jan 05 '16 at 11:24

1 Answers1

0

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.