Extract field of struct array to new array

2019-01-23 18:30发布

问题:

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?

回答1:

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 ;)



回答2:

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.