从MATLAB调用MEX中的C ++类的方法 [英] Calling methods of C++ class in MEX from MATLAB

查看:194
本文介绍了从MATLAB调用MEX中的C ++类的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DLP套件,需要使用C ++ API通过MATLAB进行控制.

I have a DLP kit which I need to control via MATLAB using a C++ API.

说,我在名为dlp_controller.cpp/.c的mex文件中使用了C/C ++ for {load_data, load_settings,display_data}的函数/方法.

Say, I have functions/methods using C/C++ for {load_data, load_settings,display_data} in a mex file named dlp_controller.cpp/.c.

我知道我可以使用MATLAB调用dlp_controller();.

I know I can call dlp_controller(); with MATLAB.

有没有一种方法可以直接从MATLAB调用此mex的方法?

Is there a way I can call the method of this mex from MATLAB directly?

说我的dlp_controller.cpp mex如下:

Say my dlp_controller.cpp mex looks as:

class dlp{ ... }
dlp::dlp{ ... }
dlp::load_data{ ... }
dlp::load_settings{ ... }
dlp::display_data{ ... }

void mexFunction(int nlhs, mxArray *[],int nrhs, const mxArray *prhs[]{ ... }

我可以以某种方式从MATLAB中调用类似dlp_controller.load_data的方法吗?注意:一种解决方法是将变量发送到dlp_controller并使用该变量和传递的数据在内部调用该函数.

Can I somehow call the methods like dlp_controller.load_data from MATLAB? NOTE: A workaround can be to send a variable to dlp_controller and call the function internally using that and the data passed.

推荐答案

AFAIK,没有做到这一点的直接方法,因为mexFunction界面相当 flat .但是,我可以想到一些不同的解决方法来使您接近.根据需要选择最佳的.

AFAIK, there's no straightforward way to do this, because the mexFunction interface is rather flat. However, there are a few different workarounds I can think of that should get you close. Choose the best one depending on your needs.

  1. 最简单的方法是在mex函数中创建dlp类的全局实例.使mex函数的第一个参数调用一个字符串,该字符串指示要调用全局对象的哪个成员函数.明显的缺点是您已将mex函数变成单例.

  1. The easiest is to create a global instance of the dlp class in your mex function. Make the first parameter of the mex function call a string that instructs which member function of the global object is to be invoked. The obvious drawback is that you've turned your mex function into a singleton.

如果需要多个dlp实例,则可以在mex函数(例如std::map<std::string, dlp>)中创建一些全局容器,然后在MATLAB中使用某些名称引用每个dlp实例.例如,要创建一个新实例,您可以使用map中不存在的名称调用mex函数.然后,您可以使用此名称,包含指示要调用的成员函数的字符串以及要传递给成员函数的任何参数的字符串来调用mex函数.还要设置一些约定,您可以通过这些约定从map中删除dlp实例.

If you need more than one dlp instance, you could create some global container in the mex function, std::map<std::string, dlp> for instance, and then refer to each dlp instance by some name from within MATLAB. For instance, to create a new instance you'd call the mex function with a name that doesn't already exist in the map. Then you could call the mex function with this name, a string indicating which member function to invoke, and any parameters to be passed to the member function. Also set up some convention by which you can erase a dlp instance from the map.

类似于第二种解决方案,您可以为每个实例返回 handles 而不是命名dlp实例.例如,创建一个全局std::set<dlp *>,当您具有mex函数时,创建一个新的dlp实例,将其添加到set并将指向分配对象的指针的副本返回到MATLAB(将其保存在标量中)类型为mxUINT64_CLASS的变量).随后调用该对象上的成员函数将把这个 handle 变量从MATLAB传递给mex函数,您将其适当地转换到mex文件中,在set中找到并调用成员函数.

Similar to the second solution, instead of naming the dlp instances you could return handles to each instance. For instance, create a global std::set<dlp *> and when you have the mex function create a new dlp instance, add it to the set and return a copy of the pointer to the allocated object to MATLAB (stick it in a scalar variable of type mxUINT64_CLASS). Subsequent calls to invoke member functions on that object will pass this handle variable to the mex function from MATLAB, you'll cast it appropriately within the mex file, find it within the set and invoke member functions.

这些方法都不是特别漂亮的方法,但是我知道这些是从mex文件中调用C ++类的成员函数的唯一方法.

None of these methods are particularly pretty, but these are the only ways I know of to invoke member functions of a C++ class from within a mex file.

这篇关于从MATLAB调用MEX中的C ++类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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