MATLAB: Do I receive an error about the structure being empty when I try to create a nested structure pointer using LIBSTRUCT in MATLAB 7.10 (R2010a)

emptylibstructMATLABpointer

I am loading a C DLL into MATLAB using LOADLIBRARY to call a function that takes a nested structure as input.
The structure is defined as follows:
typedef struct {
int one;
int two;
int three;
} s_A;
typedef struct {
s_A a1;
s_A a2;
} s_B;
The function exported from the DLL is of the form:
void my_c_function(int *a, s_B *b);
which requires me to construct a structure of type s_B.
In MATLAB 7.8 (R2009a), when I create the structure, I use the following code:
matlab_struct = libstruct('s_B');
matlab_struct.a1.one = 1;
matlab_struct.a1.two = 2;
matlab_struct.a1.three = 3;
matlab_struct.a2.one = 4;
matlab_struct.a2.two = 5;
matlab_struct.a2.three = 6;
However, in MATLAB 7.10 (R2010a), the same code errors out at the line:
matlab_struct.a1.one = 1;
saying: ERROR: ??? A dot name structure assignment is illegal when the structure is empty. Use a subscript on the structure.

Best Answer

In MATLAB 7.10 (R2010a), nested structures are no longer automatically allocated pointers on using LIBSTRUCT, which causes this error to occur.
In order to work around this issue, create the pointer to the nested structures manually before assigning values to them. Therefore, the code must be modified to the following:
matlab_struct = libstruct('s_B');
matlab_struct.a1 = libstruct('s_A');
matlab_struct.a2 = libstruct('s_A');
matlab_struct.a1.one = 1;
matlab_struct.a1.two = 2;
matlab_struct.a1.three = 3;
matlab_struct.a2.one = 4;
matlab_struct.a2.two = 5;
matlab_struct.a2.three = 6;
Note that passing nested structures via the MATLAB shared library interface is not officially supported.
See 'Limitations Using Structures' at the following location:
 
"Limitations Using Structures
Nested structures or structures containing a pointer to a structure are not supported. However, MATLAB can access an array of structures created in an external library."
Related Question