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

5 次查看(过去 30 天)
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 个评论
Friedrich
Friedrich 2014-9-10
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 2017-9-18
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?

请先登录,再进行评论。

回答(1 个)

Telencephalon
Telencephalon 2019-1-12
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

类别

Help CenterFile Exchange 中查找有关 Get Started with Microsoft .NET 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by