# Nesting depth and the error "Expected one output from a curly brace or dot indexing expression, but there were x results."

9 visualizzazioni (ultimi 30 giorni)
z8080 il 18 Ago 2022
Commentato: Stephen23 il 19 Ago 2022
Consider this toy example:
a(1).x.y=1
a(2).x.y=2
[a.x.y]
Why does this produce the infamous error
Expected one output from a curly brace or dot indexing expression, but there were 2 results.
..instead of just listing all values from across all indexed structure elements, as in this other example where the nesting is at level 2 instead of level 3:
a(1).x=1
a(2).x=2
[a.x]
>> [a.x]
ans =
1 2
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Stephen23 il 19 Ago 2022

Accedi per commentare.

### Risposta accettata

Bruno Luong il 19 Ago 2022
Modificato: Bruno Luong il 19 Ago 2022
a work around if you insist on oneline
a(1).x.y=1
a = struct with fields:
x: [1×1 struct]
a(2).x.y=2
a = 1×2 struct array with fields:
x
axy = [struct([a.x]).y]
axy = 1×2
1 2
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Più risposte (1)

Jan il 19 Ago 2022
R2022a creates a different error:
a(1).x.y=1;
a(2).x.y=2;
[a.x.y]
Intermediate dot '.' indexing produced a comma-separated list with 2 values, but it must produce a single value when followed by subsequent indexing operations.
"instead of just listing all values from across all indexed structure elements"
Think twice. [a.x] is an array already with 2 elements. The dot operator cannot handle an array as input, but a scalar struct only. This is plausible. Consider, that there is no logical decision for the dimensions of the output. It is also unclear, what you call "just listing all values".
##### 3 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Image Analyst il 19 Ago 2022
Is there a bracket/brace/parentheses solution to this, or is the only way a simple but intuitive for loop
a(1).x.y=1;
a(2).x.y=2;
all_y = zeros(numel(a), 1);
for k = 1 : numel(a)
all_y(k) = a(k).x.y;
end
or possibly a cryptic call to structfun or some other weird function
Stephen23 il 19 Ago 2022
"The dot operator cannot handle an array as input, but a scalar struct only."
???
a(1).x.y=1;
a(2).x.y=2;
tmp = [a.x] % array struct, not scalar struct
tmp = 1×2 struct array with fields:
y
[tmp.y] % dot indexing accepts an array without any problem
ans = 1×2
1 2

Accedi per commentare.

### Categorie

Scopri di più su Matrix Indexing in Help Center e File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by