Calling methods of C++ class in MEX from MATLAB

后端 未结 5 2130
一向
一向 2021-01-03 09:59

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

Say, I have functions/methods using C/C++ for {load_data, load_settings,display_data}

5条回答
  •  半阙折子戏
    2021-01-03 10:30

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

    2. If you need more than one dlp instance, you could create some global container in the mex function, std::map 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.

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

    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.

提交回复
热议问题