2

Hope, that I'm not creating a duplicate, but so far I didn't find the right answer for my problem.

Let's say, we have the following structure(s):

a(1).b = 1;
a(1).x.y = 2;

a(2).b = 3;
a(2).x.y = 4;

When I now try to get all values of b, I can do:

>> a(:).b

ans = 1
ans = 3

But how to this with the nested struct .x.y?

>> a(:).x.y

Expected one output from a curly brace or dot indexing expression, but there were 2 results.

Thanks for your help...!

2
  • Interesting to know that Octave support multiple consecutive indexing, so with octave you can use [[a.x].y] or [a.x].y. Commented Mar 27, 2019 at 9:31
  • Also if someone know why this has not be implemented in matlab: I'm interested. Commented Mar 27, 2019 at 9:34

1 Answer 1

5

Just loop over the indices.

>> arrayfun(@(k) a(k).x.y, 1:numel(a))

ans =

     2     4

or:

>> struct2array(cell2mat(extractfield(a,'x')))

ans =

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

1 Comment

Thanks! I was aware of ´arrayfun´ but didn't find the right way how to implement it.

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.