MATLAB: Array of user defined types with external libraries

external librarylibpointerlibstruct

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

Best Answer

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