How can I access nested MATLAB structure fields in my MEX program?

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.

 采纳的回答

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

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by