Array of user defined types with external libraries
조회 수: 5 (최근 30일)
이전 댓글 표시
Hi
I have a struct defined in my external c-library, e.g. :
struct MyStruct
{
char a;
short b;
}
Then I want to pass an array of these structs to my function
function myFunction1(struct MyStruct* str,int sz)
{
int kk;
for(kk=0;kk<sz;kk++)
{
str[kk].a = 1;
...
}
}
How can I do this using libpointer and libstruct? Can one only use arrays of basix types?
Thank you for any help! Thor Andreas
댓글 수: 0
채택된 답변
Philip Borghesani
2012년 12월 17일
If you have at least R2009a then there is limited support for arrays of structures. Create a matlab structure array first then create a 'lib' object from it. This code shows how you can create and index into an array of structures in MATLAB. You should be able to pass either the created libstruct or the pointer to it to your function.
fileroot=fullfile(matlabroot,'extern','examples','shrlib','shrlibsample');
loadlibrary([fileroot,'.',mexext],[fileroot,'.h'])
ms(1).p1=1.1;
ms(2).p1=1.2;
ms(1).p2=2.1;
ms(2).p2=2.2;
ls=libstruct('c_struct',ms);
pls=libpointer('c_struct',ls); % OR pls=libpointer('c_struct',ms)
pls2=pls+1;
pls2.value
댓글 수: 0
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Call C from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!