1

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?

2 Answers 2

7

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
Sign up to request clarification or add additional context in comments.

Comments

4

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.

1 Comment

[Solution does not require Mapping Toolbox](HTTP://stackoverflow.com/a/35456196/4953146)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.