MEX - multiple export functions

조회 수: 4 (최근 30일)
Jitesh Butala
Jitesh Butala 2012년 4월 23일
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