I got the following Problem: I got a struct Array and want to extract one field from that struct in a vector.
The struct has 5 fields, one which is called "name". How can I get These in a vector?
The answer by dfri works but requires MATLAB Mapping Toolbox. You can use
{yourStruct.name} to get them as a cell array or [yourStruct.name] to get them as an array:
>> A(1).name='a';
>> A(2).name='b';
>> A(3).name='c';
>> {A.name}
ans =
'a' 'b' 'c'
or,
>> A(1).num=10;
>> A(2).num=5;
>> A(3).num=25;
>> [A.num]
ans =
10 5 25
You can make use of the extractfield method:
yourNameFieldsAsArray = extractfield(yourStruct, 'name')
Where yourNameFieldsAsArray will be a cell array if the name field holds e.g. character/string values, or a regular value array if name field just hold, say, integers.