how to extract data from a cell array of structures in matlab

Assuming that each structure within the cell array has the same fields, you can extract the data from a specific field by indexing into the cell array and then into the field. Here's an example:

main.m
% Create a cell array of structures
myCellArray = {struct('field1', 1, 'field2', 'a'), ...
               struct('field1', 2, 'field2', 'b'), ...
               struct('field1', 3, 'field2', 'c')};

% Extract the field1 data from the first structure
myData = myCellArray{1}.field1;

% Extract the field2 data from all structures using a loop
for i = 1:length(myCellArray)
    myData(i) = myCellArray{i}.field2;
end
415 chars
13 lines

In the first example, we use curly braces to index into the first structure in the cell array, and then use dot notation to get the data from the field1 field.

In the second example, we use a loop to iterate through each structure in the cell array, and use the same indexing technique to get the data from the field2 field for each structure. The resulting data is stored in a vector.

gistlibby LogSnag