Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


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

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 ]
Cody

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

Philip Borghesani <philipb@mathworks.com> wrote in message <jpohob$m9s$1@newscl01ah.mathworks.com>...
> On 5/23/2012 5:17 PM, Cody wrote:
> > 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.

>
> You may be able to access the library directly with loadlibrary which
> can deal with opaque pointers. If your library has functions or
> interfaces that cannot be handled by loadlibrary you can create a helper
> library to handle interface issues and use loadlibrary on that. A
> helper library can do anything that a mex file can do and more.


Thanks for the suggestions! I was able to pass the pointer created in the mex functions by a lower-level C library back to the Matlab environment in an appropriately sized mxArray and then back into a different mex function as suggested. I was also able to declare a persistent memory location in the mex-file C-code so that sucessive invocations of the same mex function could access the pointer.



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

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.