I have a struct called A which has a [1x1] struct called B, and when I run A.B in the command line, it returns two types of answers:
ans = X: 3 Y: 2 Z: 4
ans = X: 1 Y: 5 Z: 9 W: 4 V: 2EDIT: What I'm trying to do is put all the values of X into a vector, so that the vector would look something like this: [3, 1, ...]
To do so, I'm first creating a vector called AB so that I can put [AB.X] into a another vector that I can use.
My current strategy is running a for loop like this one:
for idx = 1:length(A) AB(idx) = [A(idx).B];
endBut I get an error, "Subscripted assignment between dissimilar structures." I think that's because some values of B have 3 parts while others have 5.
How can I resolve this?
Or is there a way better than using a for loop?
21 Answer
The structs can't be concatenated together with different fields. One thing you can do to get to X is place all the struct elements into a cell array and then call cellfun to dereference X:
ABX = cellfun(@(c) c.X, {A.B}) Resources:
4