The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Pass arbitrary pointer type between mex functions
Replies: 6   Last Post: Dec 4, 2012 6:24 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 15
Registered: 9/27/10
Re: Pass arbitrary pointer type between mex functions
Posted: May 23, 2012 10:58 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"James Tursa" wrote in message <jpjnot$i91$>...
> "Cody" wrote in message <jpjk4j$41m$>...
> > Hello, hopefully some of you will be able to shed some light on the problem. I'm trying to pass a user defined pointer type between calls to different mex functions in a Matlab script. More specifically, I have some custom hardware that is accessed over USB using an API written in C. In order to access the hardware, a pointer (of a user defined type) to a device handle is generated in C like this:
> >
> > usb_device_handle=open_usb_device();
> >
> > I can then use this device handle to read 'Nbytes' amount of data from the USB device (again in C):
> >
> > usb_device_data=read_data_from_usb_device(usb_device_handle, Nbyts);
> >
> > I'd like to pass this device handle back to Matlab as an opaque type (with respect to Matlab) and then from Matlab pass the device handle to another mex function which performs other operations using the device. The problem is that Matlab only allows one to pass back types of MxArray. I've read about people using mxMalloc() and mexLock() to do these types of things... but I was wondering if there was a clean solution that I haven't considered. Again, the data type is completely opaque to Matlab, and I only intend to manipulate the pointer value in the C-code defined in the mex files.

> I don't know of a clean solution to this. Typically I would advise keeping everything in one mex file, and then invoking the mex file with various options to do the opening, reading, and closing etc. But if you really want to pass the handle back to MATLAB for some reason, you can simply put it as a data item in a uint64 class mxArray and pass that back and forth.
> James Tursa

Thanks for the response James. Since there's not an elegant solution to pass this pointer back to the Matlab workspace, do you know if there is a way I can store the user defined pointer in a memory location so that different mex functions, called separately from the Matlab command line can access this memory location and therefore the pointer?

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.