3

I have a C++ file that:

  • starts the matlab engine
  • calls matlab_optimize() (a compiled m file that runs one of matlab optimizers internally)
  • prints the result
  • stops the engine and quits

This works fine. I now want to change the second line into

  • calls matlab_optimize(obj_fun)

Where obj_fun() is a function defined in my C++ code which itself will callback into other code. Essentially I want the matlab optimizer used internally in matlab_optimize to use my supplied function pointer as the objective function.

I cant just compile obj_fun() as a standalone mex file since I want it to communicate with the c++ process that starts the matlab engine (which drives the whole thing).

A newsgroup post from 2009 seems to indicate this is not possible. Then again the Matlab C++ Math Library Toolbox does seem to be able to do this.

Googling around also reveals this generated snippet:

/*
 * Register a function pointer as a MATLAB-callable function.
 */
extern void mexRegisterFunction(void);

Which seems exactly what I want but the file is from 2000, and I find no reference to this function in the matlab docs anywhere. So how to use this?

Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
dgorissen
  • 6,207
  • 3
  • 43
  • 52
  • 1
    Just a suggestion, do you think you could live with them communicating via sockets? – enobayram Jan 31 '12 at 09:59
  • I could get things working over sockets but at this point its not really worth the effort so I would just give up on this route. – dgorissen Jan 31 '12 at 10:59

5 Answers5

5

You can use mclCreateSimpleFunctionHandle function from the mclmcrrt.h header to make this feature.

It сonverts a function with a prototype void(*) (int, mxArray*, int, mxArray) to the mxArray structure.

You can pass it to the MATLAB side function and call it like general MATLAB functions without any manipulations with mex files.

On the C/C++ side:

void callback(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{
   <some manipulations with data>;
}

...
//calling the matlab function
matlab_function(mclCreateSimpleFunctionHandle(callback));

On the MATLAB side:

function [] = matlab_function(function)
    function(<any variable>)
end
totoro
  • 161
  • 2
  • 7
2

I'd like to thank totoro for his helpful comment, here some more detailed implementation example on C++ side:

void fromMatlabCallback(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{
  cout << "WOW I'm from Matlab. and it passes me a param: ";
  int aa = mxGetScalar(prhs[0]); // it is first param; nrhs tells how many there are
  cout << aa << "\n";
}

void InitializingFunc()
{
  mxArray *func_ptr = mclCreateSimpleFunctionHandle(fromMatlabCallback);
  mxArray *retVal_ptr = NULL;
  mlfUntitled(1, &retVal_ptr , func_ptr); //Untitled is name of my main Matlab func
}
  • Do you think you could provide a full working example here? I'm not sure how I should be compiling and running this example. – mu7z Aug 27 '17 at 18:55
  • Could we get a full minimal working example and how to compile and run the example? Thanks @totoro – mu7z Oct 16 '17 at 21:04
2

I contacted Mathworks about the issue and managed to get it all working. This question was part of a wider effort of being able to pass callbacks to Python functions directly to Matlab.

Full details on this blog post and code available on github.

dgorissen
  • 6,207
  • 3
  • 43
  • 52
  • What's not clear to me is: are you using the MEX interface here or the Engine interface (they're different)? The Engine interface would let you call MATLAB from Python. Do you need to do this, or do you only need to call Python from MATLAB? I have a similar problem, where I need bidirectional communication: I need to invoke MATLAB (evaluate MATLAB code) from within language X, but I need MATLAB to be able to call back to language X during such an evaluation. – Szabolcs Apr 04 '13 at 22:21
  • I used the mex interface. For me the trick to getting bi-directional communication working was that you could simply pass around function handles (pointers) as ints. – dgorissen Apr 08 '13 at 11:21
  • I also got it working finally, using a pointer encoded as `uint64`. I used a pointer to a C++ object which had several virtual functions. The object was created in the first MEX file which managed the communication channel and the other MEX files accessed it through this pointer and called it virtual functions. Thanks for the hints! – Szabolcs Apr 08 '13 at 13:10
0

If there is a way to do that, I've never seen it. To make matters worse, the Matlab C++ Math Library you reference no longer exists:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/267802

Jonathan
  • 616
  • 4
  • 7
0

It seems that you can create a c-linkable library from any MATLAB function (see here). If this works as advertised, I think you should be able to do what you want though in a different way.

Mosby
  • 1,291
  • 1
  • 10
  • 14
  • this is what I did to the matlab_optimize(), compile it to a shared library which I can then interface with other native code. Problem is how do I pass a function handle to it. – dgorissen Jan 31 '12 at 13:00