11

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?

2 Answers 2

18

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

Sign up to request clarification or add additional context in comments.

6 Comments

What about string values ? if poses.time is ans="1" ans="2" ans="3", timevec=[poses.time] would give "123". Is there a way to fix this ?
@zml "123" is already an array, try indexing it and see for yourself. If you want them really separate, you'll need a cell array: timevec={poses.time}. See here for more on string arrays
What if poses is 2d?
@Eric the brackets [...] on [poses.time] will concatenate it in a 1D array, regardless of the size of poses (as long as the actual values cán be put in a 1D array). Use reshape to get it back into the shape you started with
So in general, reshape(mystruct.member, size(mystruct))?
|
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.

Comments

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.