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

Thread Subject:
Pass arbitrary pointer type between mex functions

Subject: Pass arbitrary pointer type between mex functions

From: Cody

Date: 23 May, 2012 21:17:07

Message: 1 of 7

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.

Subject: Pass arbitrary pointer type between mex functions

From: James Tursa

Date: 23 May, 2012 22:19:09

Message: 2 of 7

"Cody" wrote in message <jpjk4j$41m$1@newscl01ah.mathworks.com>...
> 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

Subject: Pass arbitrary pointer type between mex functions

From: Cody

Date: 24 May, 2012 02:58:08

Message: 3 of 7

"James Tursa" wrote in message <jpjnot$i91$1@newscl01ah.mathworks.com>...
> "Cody" wrote in message <jpjk4j$41m$1@newscl01ah.mathworks.com>...
> > 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?

Subject: Pass arbitrary pointer type between mex functions

From: Bruno Luong

Date: 24 May, 2012 06:37:08

Message: 4 of 7

"Cody" wrote in message <jpk840$ko1$1@newscl01ah.mathworks.com>...

> Thanks for the response James. Since there's not an elegant solution to pass this pointer back to the Matlab workspace,

Why not cast the pointer to uint64, or double, or 1x8 uint8 array (whatever that has the same size as a ppointer) and return it to MATLAB as mxArray?

>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?

Just pass the pointers in mxArray as I explain above.

Brunp

Subject: Pass arbitrary pointer type between mex functions

From: Philip Borghesani

Date: 25 May, 2012 18:07:07

Message: 5 of 7

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.

Subject: Pass arbitrary pointer type between mex functions

From: Cody

Date: 26 May, 2012 21:00:30

Message: 6 of 7

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.

Subject: Some hints or code snippets of the solution.

From: Chris

Date: 4 Dec, 2012 11:24:08

Message: 7 of 7

"Cody" wrote in message <jprg9e$184$1@newscl01ah.mathworks.com>...
> 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.

Hey Cody,

I'm facing exactly the same problem: I have a USB device to which I can transmit data via a provided driver. I call the driver's functions within a mex-File and want to open the device, transmit data and eventually close the device. The handle which I receive from the open-function (of the driver) is an unsigned long variable. However, I'm quite frustrated because I'm not able to pass this device handle back to Matlab so as to call another mex-function which internally calls the driver function to transmit data.
So far, I've tried to create a persistent variable using mxCalloc and mxMakeMemoryPersistant but this approach does not result in the desired functionality.

I would really appreciate it if you give me a hint or some code snippets explaining your solution of this issue.

Chris

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us