Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract field of struct array to new array

I have a struct, which has 2 fields: time and pose. I have multiple instances of this struct composed in an array, so an example of this is:

poses(1)
    -time = 1
    -pose = (doesn't Matter)
poses(2)
    -time = 2
    -pose = (doesn't Matter)
poses(3)
    -time = 3
    -pose = (doesn't Matter)
...

Now when I print this:

 poses.time

I get this:

ans =
      1
ans =
      2
ans =
      3

How can I take that output and put it into a vector?

like image 253
Fantastic Mr Fox Avatar asked Aug 22 '12 23:08

Fantastic Mr Fox


2 Answers

For cases that the field values are vectors (of same size) and you need the result in a matrix form:

posmat = cell2mat({poses.pose}');

That returns each pose vector in a different row of posmat.

like image 86
saastn Avatar answered Sep 25 '22 04:09

saastn


Use brackets:

timevec=[poses.time];

tricky matlab, I know I know, you'll just have to remember this one if you're working with structs ;)

like image 42
Gunther Struyf Avatar answered Sep 22 '22 04:09

Gunther Struyf