How to use indexing in complex structure

192 views (last 30 days)
I have been trying for hours to understand how to index into a complex structure. None of the examples I find online seem to match the issue I am having and suggest what I am doing ought to work. If someone could direct me to information on how to index into structures that is more advanced than just the how to index into a structure page Structure Arrays (which seems to suggest I ought to be able to index the way I am trying to do), that would be amazing.
The structure (A or "out") has multiple elements, including one structure (a or "elecs") inside it which has Field columns with names and then each cell in the column I am trying to index (b or "arts") either has an empty array [] or an array of 30 rows and 1 column (a double).
I tried using A.a.b(10) for example to get the array out of the 10th row of A.a.b as per the documentation on structures, and have tried A.a.b(10,:) and also tried using [], {}, combinations of these, etc, but no matter what I do, I get an error that says, "Intermediate dot '.' indexing produced a comma-separated list with 256 values, but it must produce a single value when followed by subsequent indexing operations." This is the same whether I try to indext into a cell with an empty array [], a single number, or a 30x1 double. I have no idea where it is getting 256 comma separated values for A.a.b(10) (although A.a has 256 rows in total, nothing in A.a.b would really have 256 values and there is no 10th column in A.a).
A screenshot of the actual structure is here, but my question is really about how to index into a structure in a structure at all in general. Nothing I am doing seems to work, and every example online that I can find seems to suggest using the syntax I have been trying. So I am frustrated.
  1 Comment
Stephen23
Stephen23 on 22 Jul 2023
Edited: Stephen23 on 22 Jul 2023
" If someone could direct me to information on how to index into structures that is more advanced than just the how to index into a structure page Structure Arrays (which seems to suggest I ought to be able to index the way I am trying to do), that would be amazing."
A common misunderstanding of nested structures is that they are somehow one structure: they are actually separate structure arrays that just happen to stored inside other structure arrays. This means if ELEC has multiple elements then you need to index into, then you need to index into ELEC, not some other array (structure or not). For example: out.elecs(10).arts.
"The structure (A or "out") has multiple elements, including one structure (a or "elecs") inside it ..."
The screenshot and also Voss' answer indicates that actually OUT must be a scalar structure, i.e. have exactly one element. An array with one element is scalar. How many fields it has we don't know, but it has atleast one named ELECS.
"...which has Field columns with names and then each cell in the column I am trying to index (b or "arts") either has an empty array [] or an array of 30 rows and 1 column (a double)."
You are confusing how structures are displayed by the variable viewer / workspace. For non-scalar structures the elements (i.e. what you can refer to using indexing) are listed as rows of that table from 1... N. The header along the top are the fieldnames, which is what "Field" refers to. I agree that the "Fields" header is confusing, but that is unfortunately how it is.
When working with non-scalar structures you need to keep a very clear idea of how the data is arranged: do not mix up elements, fields, or the nested structures themselves. Use SIZE/NUMEL and FIELDNAMES to get the number of elements and fields respectively: don't guess, check each structure to know its size and fields.

Sign in to comment.

Accepted Answer

Voss
Voss on 22 Jul 2023
If you want the value of the arts field in the 10th element of out.elecs, you would say:
out.elecs(10).arts
  1 Comment
Caren Armstrong
Caren Armstrong on 22 Jul 2023
That was shockingly simple, yet never occurred to me to index that way. Thank you very much!

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!