MEX - multiple export functions

3 次查看(过去 30 天)
Hello All,
I would like to export multiple functions from a single MEX file. basically, what I am trying to achieve is to have a single .dll file which will have the functions to connect to a proprietary piece of hardware, then exchange data with the hardware and in the end close down the connection. The connection and disconnection will happen only once whereas the exchange of data will happen multiple (unknown) times in between these calls. Hence, I cannot use a single function to do all this work.
In the same vein, if I wrote a C++ class to do all this, can I get the convenience and functionality of OOPS in MATLAB? Or do I need to write a wrapper MATLAB class for the same?
I know that these might be multiple queries in the same post, but would be glad for any help that I can get.
Thanks, Jitesh Butala
  1 个评论
Jitesh Butala
Jitesh Butala 2012-4-23
EDIT : Currently, I have created a workaround for the multiple functions that need to be exported. I am using the same mexFunction function with an additional input parameter which specifies which of the events needs to be executed.
Now, I have come to a point where I need memory to be static across multiple calls of the mexFunction since we need to use the same memory allocated in the first connect call in order to avoid memory leak. Does anyone have any thoughts on this?

请先登录,再进行评论。

采纳的回答

James Tursa
James Tursa 2012-4-23
To have your variables retain values from call to call, put them at the highest level of your code (i.e., outside any function scope so that they are global). If you are dynamically allocating memory, you will need to use mexAtExit to register an exit function to deallocate all of your dynamically allocated memory (and any other necessary cleanup) if the mex function gets cleared. If you are using the MATLAB API functions (e.g., mxMalloc) to allocate the memory, then you will also need to use mexMakeMemoryPersistent (or mexMakeArrayPersistent for mxArray variables) to prevent MATLAB from freeing the memory between mex routine calls. C++ class variables cannot be directly returned to the MATLAB workspace ... you will need to copy the relevant data from the C++ class variable to an mxArray variable.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by