Why Do I Get This Error for This Non-scalar Structure?
42 views (last 30 days)
Rightia Rollmann on 13 Mar 2017
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
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.”
WHY do I get this error and MATLAB avoids executing this code?
Stephen23 on 13 Mar 2017
Edited: Stephen23 on 13 Mar 2017
Comma separated lists are really very simple. You use them all the time. Here is one:
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:
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:
and from a structure:
But both of these are still exactly equivalent to what I wrote at the top: they will generate this:
and remember NOT ONE VARIABLE. Therefore the syntax you used:
is an error because
creates a comma-separated list, exactly equivalent to this:
and it is ambiguous what role the .C should have on the end.
is exactly equivalent to writing this list of independent variables:
and yet would you expect to write this list of independent variables:
and expect it to get the C field of X, Y, and Z?
More Answers (3)
Jan on 14 Mar 2017
Edited: Jan on 14 Mar 2017
Fllowing your comment: A(:).B.C not work?"
Because the operation ".C" is defined for structs and struct arrays, but not for a comma separated list. In A.C the opearion ".C" is applied to a struct array. In A(1).B.C it is applied to a struct. And in A(:).B.C it is tried to be applied to a comma separated list, but this is not defined.
You've struggeled a lot with nested structs. Although this might not be intuitive, Matlab does not have vectorized methods to process nested structs. This requires for loops and there is no way around - except for not using nested structs.
Of course it would be possible to implement this. I thought of doing this in a fast C-mex function, but then I stopped this due to ambiguities: What is the wanted result, when the nested struct contains struct arrays in different levels?
Or if C is a cell containing structs? How sould X look like? A general method must be able to catch this in an intuitive way, and this is not possible in my opinion. Therefore a function for accessing nested structs might look smart at first glance, but I'm convinced that it produces confusion and impedes the debugging. Therefore I stopped the development and decided to rely on stupid loops. Even if this might run some percent slower. Elegance of the the code does not rule, when it causes horrible debugging sessions.
Adam on 13 Mar 2017
returns a comma-separated list of (in your example) 3 objects. You can't then further index these by adding .C at the end. Try just putting A(:).B on your command line and you will see why.
Mehmet Burak Ekinci on 18 Aug 2022
If the problem is concatenating array elements of A struct's B.C field.
You could use this custom matlab functions from file exchange. It is for getting values and plotting for nested structure arrays.
For your example:
>>A(1).B.C = 'a';
A(2).B.C = 'b';
A(3).B.C = 'a';
3×1 char array