MATLAB: How to extract the field elements of a structure contained in a cell array and store them in a vector

MATLAB

I have a cell array where each cell contains a structure. I want to extract all of the values of a specific structure field and store them in a numeric array. The following is an example of the structure:
myArry{1}.position = 'e';
myArry{1}.color = 'red';
myArry{1}.index = 1;
myArry{2}.position = 'w';
myArry{2}.color = 'orange';
myArry{2}.index = 2;
myArry{3}.position = 'se';
myArry{3}.color = 'blue';
myArry{3}.index = 3;

Best Answer

It is possible to extract and concatenate the field elements of structures contained in a cell array. In order to employ this method without the use of a loop, the structure in each cell must have identical fields.
To extract the “index” field and store it in a vector, use the following syntax:
myStructArry = [myArry{:}];
myIndexArry = [myStructArry.index];