Array of user defined types with external libraries

1 view (last 30 days)
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

Accepted Answer

Philip Borghesani
Philip Borghesani on 17 Dec 2012
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

More Answers (1)

Thor Andreas
Thor Andreas on 18 Dec 2012
Thank you!

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!