Date: May 23, 2012 5:17 PM
Author: Cody
Subject: Pass arbitrary pointer type between mex functions

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.