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