array pointers in matlab

조회 수: 4 (최근 30일)
Sam Van der Jeught
Sam Van der Jeught 2011년 4월 19일
답변: Jim Hokanson 2014년 2월 14일
Hi all,
I'm trying to implement a dll that I have created in Labview into Matlab. When the output value is a simple integer or floating point number, this poses no problems. However, when the dll created in Labview exports an array as output, an array pointer is used:
void __stdcall Test(int16_t outputArray[], int32_t len); //Testfunction in header file
My question is how do I read out the data present at this point in memory, in Matlab? As said before, when the output is a single number, I can simply
loadlibrary SharedLib.dll SharedLib.h alias lib
output = calllib('lib', 'Test')
unloadlibrary lib
in Matlab, but this obviously doesn't work now. Any guidance towards a solution would be greatly appreciated.
Best regards,
Sam
  댓글 수: 1
Malcolm Lidierth
Malcolm Lidierth 2011년 4월 19일
v = zeros(10,1,'uint16');
v = calllib('lib','Test',v,int32(10));
should do the trick. MATLAB creates the libpointer automatically as I recall. On output, v is a int16 array, not a libpointer - hence no v.value property.

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

답변 (3개)

Chirag Gupta
Chirag Gupta 2011년 4월 19일
Hi Sam,
I coded up a sample library with a signature of:
void TestFunc(int outArray[],int len);
Then to use this, you need to make use of libpointers.
Sample code for using my function was:
loadlibrary('TestShared.dll','TestFile.h','alias','lib')
libfunctionsview lib
var = zeros(10,1);
% Creating a pointer from var of int32 type. In your case it would % be int16
v = libpointer('int32Ptr',var)
v.Value
v = calllib('lib','TestFunc',v,int32(10))
v.Value
unloadlibrary lib
You can find more examples with other types in the documentation
  댓글 수: 1
Kaustubha Govind
Kaustubha Govind 2011년 4월 19일
The documentation is here: http://www.mathworks.com/help/techdoc/matlab_external/f42650.html

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


Sam Van der Jeught
Sam Van der Jeught 2011년 4월 19일
Hi Chirag,
Thanks for the fast reply. I implemented your code and adapted it to suit my specifics:
loadlibrary('SharedLib.dll','SharedLib.h','alias','lib')
libfunctionsview lib
var = zeros(10,1);
% Creating a pointer from var of int32 type. In your case it would % be int16
1
v = libpointer('int16Ptr',var)
2
v.Value
3
v = calllib('lib','Test',v,int32(10))
4
v.Value
unloadlibrary lib
but I end up with the following error when I run the above code in Matlab:
ans =
1
v =
libpointer
ans =
2
ans =
0
0
0
0
0
0
0
0
0
0
ans =
3
v =
0
0
0
0
0
0
0
0
0
0
ans =
4
??? Attempt to reference field of non-structure array.
Error in ==> test at 12 v.Value
Maybe you have an idea of what still goes wrong.
Thanks in advance,
Sam
  댓글 수: 1
Chirag Gupta
Chirag Gupta 2011년 4월 19일
Can you check what exactly the function signature is returning in MATLAB.
libfunctionsview lib should show that.
Also, try whos v to check v's type after the function call.

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


Jim Hokanson
Jim Hokanson 2014년 2월 14일
Just in case anyone comes across this, I think you want the following:
For: void __stdcall Test(int16_t outputArray[], int32_t len); //Testfunction in header file
Assuming the library is referred to as 'lib'
len = 10; temp = zeros(1,len,'int16'); ptr = libpointer('int16Ptr',temp);
calllib('lib','Test',ptr,int32(len))
data = get(ptr,'Value')
The key point is that the output is void, so you don't assign an output to calllib, i.e. you don't do:
output = calllib
Since you are working with a pointer you need to go back to the pointer and extract its value.
NOTE: I wasn't sure what the value was, but if you do get(ptr) Matlab will display information about the object.

카테고리

Help CenterFile Exchange에서 LabVIEW에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by