How to access a field of a struct by indexing?
2,220 ビュー (過去 30 日間)
I have a 1-by-1 struct that possesses 3 fields named B, C, and D. Is there any way to call D by its index (i.e., D is the third field of struct A, so call the third field of struct A without mentioning the field name D) rather than its name (i.e, A.D)?
A.B = 1;
A.C = 2;
A.D = 3;
その他の回答 (1 件)
Guillaume 2017 年 2 月 26 日
Yes, there is a way to get the nth field directly:
fns = fieldnames(A);
But be aware that the order of the fields depends solely on the order in which they were created. As Jan pointed out, two structures may be indentical, yet have different field order.
Usually, you would only access fields by their index when you're doing some structure metaprogramming