MATLAB - How to resolve "Subscripted assignment between dissimilar structures" ERROR

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: 2

EDIT: 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];
end

But 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?

2

1 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:

Comma separated lists

cellfun

Anonymous functions

4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like