Get the n-th section of the structure

I need to be able to access n element of a structure in Matlab.

Let's say I have a structure defined

struc.a = 1;
struc.b = 3;
struc.c = 2;

      

I would like to be able to access the second element of the structure and get number 3 as an answer.

+3


source to share


2 answers


I actually found a slightly more elegant method than @Dan using dynamic field names .



n = 2;
fields = fieldnames(struc);
struc.(fields{n})

      

+1


source


n = 2;
fields = fieldnames(struc);
getfield(struc, fields{n})

      



+7


source







All Articles