将C/C ++回调传递到Matlab引擎 [英] Passing C/C++ callbacks into the matlab engine

查看:121
本文介绍了将C/C ++回调传递到Matlab引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++文件,

I have a C++ file that:

  • 启动matlab引擎
  • 调用matlab_optimize()(一个在内部运行matlab优化器之一的已编译m文件)
  • 打印结果
  • 停止引擎并退出
  • 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

  • 致电matlab_optimize(obj_fun)

obj_fun()是在我的C ++代码中定义的一个函数,它本身将回调到其他代码中.本质上,我希望matlab_optimize内部使用的matlab优化器将提供的函数指针用作目标函数.

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.

我不能只将obj_fun()编译为独立的mex文件,因为我希望它与启动Matlab引擎(驱动整个引擎)的c ++进程进行通信.

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

2009年的新闻组帖子似乎表明这是不可能的.然后再次使用Matlab C ++数学库工具箱似乎可以做到这一点.

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.

还可以在周围搜索该生成的代码段:

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

这似乎正是我想要的,但文件来自2000年,而我在任何地方的Matlab文档中都找不到对该函数的引用.那么如何使用呢?

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?

推荐答案

我就此问题与Mathworks进行了联系,并设法使其全部正常运行.这个问题是将回调函数直接传递给Matlab的更广泛努力的一部分.

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.

有关的完整详细信息代码可在github上获得.

这篇关于将C/C ++回调传递到Matlab引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆