Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to slice a struct array?

How can I extract a specific field from each element of a Matlab struct array?

>> clear x
>> x(1).a = 6;
>> x(2).a = 7;

I'd like an array containing 6 and 7. Neither x(:).a nor x.a do what I want.

>> x(:).a

ans =    

     6


ans =

     7
like image 859
Tim Avatar asked Dec 25 '11 19:12

Tim


1 Answers

No problem - just use :

arr = [x.a];

It will concat all of the values that you need. If you have a more complex data, you can use the curly bracers:

b(1).x = 'John';
b(2).x = 'Doe';
arr = {b.x}; 
like image 72
Andrey Rubshtein Avatar answered Oct 07 '22 19:10

Andrey Rubshtein