Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Using Mex with Classes

Asked by Sridutt Nayak on 9 Jan 2013

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 Comments

Sridutt Nayak

Products

3 Answers

Answer by James Tursa on 9 Jan 2013
Edited by James Tursa on 10 Jan 2013
Accepted answer

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 Comments

Sridutt Nayak on 10 Jan 2013

There may be a problem with this approach,

1. Initialization works fine..

2. Say I have been calling the mexFunction but after a few executions, the Matlab code that was running exits improperly(or a Ctrl+C)

3. Next time, when I run the Matlab code, the mex file that was loaded still remains persistent(it was not cleared due to a Ctrl+C) and gives an error as clear_handle would not be called as it had exited improperly.

I thought of same approach which Kaustubha Govind in 2nd method, will try and update.

Thanks.

James Tursa on 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.

James Tursa
Answer by Kaustubha Govind on 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 Comments

Kaustubha Govind
Answer by Sridutt Nayak on 10 Jan 2013
Edited by Sridutt Nayak on 10 Jan 2013

Thanks for all your help...

  • 3 Different mex files somehow did not work, may be once one of the mex executables takes the handler, there cannot be other executable using it as the OS may create its own link to the hardware through that handler.
  • I used the same pseudocode as told by James Tursa along with the static handler as mentioned by Kaustubha Govind

0 Comments

Sridutt Nayak

Contact us