MATLAB: Do I Get This Error for This Non-scalar Structure

accessing datastruct

I have this struct:
A(1).B.C = 'a';
A(2).B.C = 'b';
A(3).B.C = 'a';
When I execute the code below, it works
A(:).B
However, when I execute the code below, it throws an error: “Expected one output from a curly brace or dot indexing expression, but there were 4 results.”
A(:).B.C
WHY do I get this error and MATLAB avoids executing this code?

Best Answer

Comma separated lists are really very simple. You use them all the time. Here is one:
a,b,c,d
There is a comma separated list containing four variables, the variables a, b, c, and d. Every time you write a list of variables separated by commas then you are writing a comma separated list. Most commonly you would write a comma separated list when calling a function or operator:
fun(a,b,c,d)
It is important to note that a comma separated list is not one variable! Sometimes we want to create a comma separated list from one variable: MATLAB has two ways of doing this, these are from a cell array:
cell_array{idx}
ND_struct.field
But both of these are still exactly equivalent to what I wrote at the top: they will generate this:
variable1,variable2,variable3,...
and remember NOT ONE VARIABLE. Therefore the syntax you used:
A(:).B.C
is an error because
A(:).B
creates a comma-separated list, exactly equivalent to this:
A(1).B,A(2).B,A(3).B,...
and it is ambiguous what role the .C should have on the end.
Because this
A(:).B
is exactly equivalent to writing this list of independent variables:
A(1).B,A(2).B,A(3).B...
and yet would you expect to write this list of independent variables:
X,Y,Z.C
and expect it to get the C field of X, Y, and Z?