Array of user defined types with external libraries

5 vues (au cours des 30 derniers jours)
Thor Andreas
Thor Andreas le 17 Déc 2012
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

Réponse acceptée

Philip Borghesani
Philip Borghesani le 17 Déc 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

Plus de réponses (1)

Thor Andreas
Thor Andreas le 18 Déc 2012
Thank you!

Catégories

En savoir plus sur Call C from MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by