Using Mex with Classes
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have written a mex file to 1. Initialize hardware. 2. Get Data. 3. Close & Exit hardware.
----Some_App.cpp----
void mexFunction(parms)
{
handle = init_hw();
alloc_mem(handle);
get_data();
copy_to_mxDoubleMatrix();
exit_hw(handle);
}
This works fine.
I have to change it such a way that init_hw(); is in a different class. Using this handle, I need to be able to allocate_mem(handle), get_data()... say a 100 times, then exit_hw(handle); as the init_hw() takes long to run I don't want to init_hw() everytime.
Can I do something to separate them into classes and use like
handle = Some_App.init_hw();
for i = 1:100
array = Some_App.acquire(handle); /*All Memalloc, read go into this class*/
end
Some_App.exit_hw(handle);
Any Tutorials/Help/Links can be useful. Thanks
0 commentaires
Réponse acceptée
James Tursa
le 9 Jan 2013
Modifié(e) : James Tursa
le 10 Jan 2013
One option is to keep your pointer in a high level variable (i.e., global) and set up a mexAtExit function to clean things up when the function is cleared. E.g.,
void *handle = NULL; // not sure of type here ... you didn't declare it above
void clear_handle(void)
{
if( handle ) {
exit_hw(handle);
handle = NULL;
}
}
void mexFunction( ...parms... )
{
if( !handle ) {
handle = init_hw();
if( !handle ) {
mexErrMsgTxt("Unable to init handle.");
}
mexAtExit(clear_handle);
}
alloc_mem(handle);
get_data();
copy_to_mxDoubleMatrix();
}
Only on the first call init_hw is called and the handle remembered. When you clear this routine from memory, exit_hw is called to clean things up.
2 commentaires
James Tursa
le 10 Jan 2013
Modifié(e) : James Tursa
le 10 Jan 2013
I would say it depends on what particular errors you are talking about, and what is in your other functions that you do not show the details for. Is there something in your other code that would make "handle" invalid without NULL'ing it out? Something that causes alloc_mem to fail but you don't handle the error? Or what? As far as implementation is concerned, I am a fan of putting everything in one mex file and calling it with options to activate the different underlying functions rather than several mex files that need to communicate & pass data amongst themselves. Using a single mex file makes the coding much easier IMO. If you need to make sure things start afresh when there is an error in a previous run, simply make sure you clear Some_App before starting your new run.
Plus de réponses (2)
Kaustubha Govind
le 9 Jan 2013
There might be other solutions to this, but if you'd like to maintain the handle inside the MEX-function, you could try declaring it as a persistent array which is initialized when the MEX-file is loaded and deleted when the MEX-file is unloaded.
Another approach (that I haven't tested out), if you are able to store the handle in MATLAB code, might be to create a MATLAB class that stores the handle and has methods called init_hw, acquire, exit_hw, etc. Each of these methods might be wrappers to different MEX-functions that perform the actual operation.
0 commentaires
Voir également
Catégories
En savoir plus sur External Language Interfaces dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!