How can I access and use data in Matlab indicated by a System.IntPtr ?

1 view (last 30 days)
I am using a .NET interface supplied by the manufacturer to access a camera and aquired images. The raw data is indicated by System.IntPtr's which point to the lines of the raw image. How can I access the corresponding data (load it to a corresponding row in a matlab array?)? Thanks.
  2 Comments
Friedrich
Friedrich on 10 Sep 2014
The same way you would do it in .NET. Normally you use Marshal.Copy to copy the underlying unmanged data to a managed array. Then you would need to convert that managed array into a MATLAB array by calling the toDouble method (or Double method) [not exactly sure how it's called]
Michael George
Michael George on 18 Sep 2017
I have the same question.
Friedrich, it wasn't exactly clear to me how I am supposed to access the Marshal .NET class in MATLAB. Do I need to add a specific assembly?

Sign in to comment.

Answers (1)

Telencephalon
Telencephalon on 12 Jan 2019
I just ran into a similar problem. I was able to get the array data (of size dataLength) into Matlab from the .NET library using one copy operation (this comes at a cost; I don't think that there's a way around it though, since there doesn't seem to be a .NET-equivalent of Matlab's libpointer() function):
hglobal = System.Runtime.InteropServices.Marshal.AllocHGlobal(dataLength * 2); % assuming two bytes per element for Int16
% ... pass hglobal to the .NET function that fills it with the requested data ...
netarray = NET.createArray('System.Int16', dataLength); % allocate managed array to receive copy of unmanaged data
System.Runtime.InteropServices.Marshal.Copy(hglobal, netarray, 0, dataLength); % copy unmanaged to managed
System.Runtime.InteropServices.Marshal.FreeHGlobal(hglobal); % free unmanaged array memory
data = int16(netarray); %convert to matlab array

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!