MATLAB: How to access nested MATLAB structure fields in the MEX program

levelMATLABmultistruct

I have created a nested structure in MATLAB:
substruct.a = 1
substruct.b = 2
s.c = 3
s.sub = substruct
Now I would like to write a MEX program that can access certain levels of depth into this structure. I would like to access these fields in my MEX-file.

Best Answer

The following code fragment demonstrates how to extract a substructure and then extract a field from that substructure:
substructure_field_num = mxGetFieldNumber(pa, "substructure");
mxArray *sub = mxGetFieldByNumber(pa, index, substructure_field_num);
field_num = mxGetFieldNumber(sub, "my_field");
mxArray *myField = mxGetFieldByNumber(sub, index, field_num);
For more information on using structures in general, see the documentation for mxGetFieldByNumber and the related PHONEBOOK.C example. You can use the following command in MATLAB to access the documentation:
doc mxGetFieldByNumber