MEX - multiple export functions

3 Ansichten (letzte 30 Tage)
Jitesh Butala
Jitesh Butala am 23 Apr. 2012
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 Kommentar
Jitesh Butala
Jitesh Butala am 23 Apr. 2012
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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

James Tursa
James Tursa am 23 Apr. 2012
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.

Weitere Antworten (0)

Kategorien

Mehr zu Write C Functions Callable from MATLAB (MEX Files) finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by