Mex Programming: How can I know the length of a struct array that is a field of a parent struct?

조회 수: 3 (최근 30일)
Hello, consider the following matlab structure, with a field that is a structure array of some length:
substructlength = randi(100);
for i = 1:substructlength
root.foobar(i).a = rand;
end
I want to process 'root' from a mex file. It doesn't seem possible to know the length of the substructure 'foobar' using just the root mxArray input. I see the mxGetField documentation requires an index input, so one must know the maximum index a-priori:
mxArray *mxGetField(const mxArray *pm, mwIndex index, const char *fieldname);
How can I know the length of the 'foobar' sub-structure array using only the root mxArray?

채택된 답변

Guillaume
Guillaume 2018년 9월 19일
You misunderstood what the index refers to. It's the index of the parent structure, not of the field. So for structure
root(i1).foobar(i2).a
You'd call
mxArray* fn = mxGetField(root, i1, 'foobar'); %access field of element i1 of root
which gives you a pointer to another structure array for which you can query the size with mxgetNumberOfElements and co.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by