MATLAB: How to access a field of a struct by indexing

fieldindexingstruct

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;

Best Answer

A_cell = struct2cell(A);
D = A_cell{3}
Keep in mind that the order of the fields of structs is not necessarily constant:
A.B = 1;
A.C = 2;
A.D = 3;
B.B = 1;
B.D = 3;
B.C = 2;
isequal(A, B) % >> TRUE!