Marshal double array to MATLAB with IntPtr

I have a .Net library with a method that returns an interface with 2 members:
IntPtr PointerToFirstElement { get; }
int Count { get; }
These allow access to an array of doubles without copying. I want to plot this array in MATLAB by calling the method on the .Net library, but I can't figure out how to create a plottable MATLAB structure from my IntPtr and Count.

답변 (1개)

Guillaume
Guillaume 2018년 5월 31일

0 개 추천

I don't think matlab has any way to access directly the unmanaged memory pointed to by the IntPtr as that would normally be wrapped in an unsafe block.
All I can think of is going through the marshalling methods to make a copy to managed memory and get that through matlab. However, I'm not experienced enough with that aspect of .Net to be sure it is really going to work:
netarray = NET.createArray('System.Double', yourobject.Count); %allocate managed array to receive copy of unmanaged data
System.Runtime.InteropServices.Marshal.Copy(yourobject.PointerToFirstElement, netarray, 0, yourobject.Count); %copy unmanaged to managed
data = double(netarray); %convert to matlab array
If I got it wrong, it is very likely to crash matlab.

댓글 수: 1

William Sardar
William Sardar 2018년 5월 31일
편집: William Sardar 2018년 5월 31일
Thanks for your input. Shame if I can't access unmanaged memory - this is possible in Python with Numpy! I am trying to avoid doing a copy for performance reasons. What's more, it looks like you can access unmanaged memory when calling into a native (e.g. C++) dll, with the libpointer class.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Deploy to .NET Applications Using MWArray API에 대해 자세히 알아보기

제품

릴리스

R2012b

태그

질문:

2018년 5월 30일

편집:

2018년 5월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by