When using C structures which contain elements to pointers, one must consider that the memory for the data that the pointer points to is allocated separately from the structure.
If the structure is returned to MATLAB from a shared library function call, you will need to set the data type and size of the pointer members of the structure. For example:
sp=libpointer('my_structPtr')
calllib('structsample', 'allocateStruct', sp)
setdatatype(sp.Value.a1,'uint8Ptr',1,sp.Value.size_a1)
setdatatype(sp.a2,'doublePtr',1,size_a2)
Data pointed at by the structure can now be accessed. Note that for the character data, you need to convert the raw ASCII values to printable characters using the CHAR function.
char(sp.Value.a1)
sp.Value.a2
When passing a structure to a shared library function, the members of the structure need to be initialized. For example:
s.a1=uint8(['hi' 0]);
s.size_a1=length(s.a1)
s.a2=1:0.1:10
s.size_a2=length(s.a2)
get(s)
calllib('structsample', 'readMyStruct', s)
Note: Character data needs to be converted to integer data in order to be assigned to the "a1" char pointer data member.
Best Answer