Problem with accessing struct in a struct

2 ビュー (過去 30 日間)
Benjamin Pommer
Benjamin Pommer 2022 年 9 月 8 日
コメント済み: Stephen23 2022 年 9 月 8 日
Hi Matlab community
I have the following problem. I created a code which does system identification with various samples of simulation data to get various output error models. In order to save all of them I created the following code:
for i = 1:laenge
sys = oe(data,[1 2 0]);
sys_oe.A = sys.A;
sys_oe.B = sys.B;
sys_oe.C = sys.C;
sys_oe.D = sys.D;
sys_oe.F = sys.F;
sys_oe.NoiseVariance = sys.NoiseVariance;
sys_oe.Ts = sys.Ts;
oe_sum.numerierung_str(i) = sys_oe;
numerierung_str is a string array which using letters to numerate starting from 'a', 'b', ...
The idea was to access the individual parameters (sys_oe.A, ...) of each model and rebuilt it through idpoly by using the command oe_sum.a.A etc.
In my case, in the workspace there is the "variable" oe_sum which contains one field "numerierung_str" and one value "1x19 struct". When I open that I get a kind of table (which is actually a structure array).
Why didnt my idea work out accessing the variable/array in the struct in the struct by using the command oe_sum.a.A for example?
Using the command oe_sum.numerierung_str.A(1) or oe_sum.numerierung_str.A(1,1) gives me the following error:
"Intermediate dot '.'
indexing produced a
comma-separated list with 19
values, but it must produce
a single value when followed
by subsequent indexing
Related documentation"
When I type "oe_sum.numerierung_str(1)" in the Workspace, the following is displayed:
struct with fields:
A: 1
B: 0.0552
C: 1
D: 1
F: [1 … ]
NoiseVariance: 1.2973e+05
Ts: 0.0100
How can I access single variables here?


Stephen23 2022 年 9 月 8 日
編集済み: Stephen23 2022 年 9 月 8 日
"Why didnt my idea work out accessing the variable/array in the struct in the struct by using the command oe_sum.a.A for example?"
Because you actually used indexing into a hard-coded fieldname (resulting in a non-scalar structure nested inside a scalar structure), rather than the dynamic fieldname (of a scalar structure) that you thought you were doing.
To achieve what you want, you need to use dynamic fieldnames:
For example, replace this:
oe_sum.numerierung_str(i) = sys_oe;
with this:
oe_sum.(numerierung_str(i)) = sys_oe;
% ^ ^ dynamic fieldname
However I recommend simplifying your code and data by using just one non-scalar structure. For example:
C = cell(1,laenge);
for k = 1:laenge
C{k} = oe(data,[1,2,0]);
S = vertcat(C{:}) % assuming all structures returned by OE have the same fields
All of your data will be in the non-scalar structure S. For example, the second iteration's data will be:
This will be much simpler to work with, for example you can use this convenience syntax:
  3 件のコメント
Stephen23 2022 年 9 月 8 日
"for some reason the A coefficients are save as a matrix with the dimensions laenge x laenge instead of a vector laenge x 1."
I have no idea what the "A coefficients" are.
"Why is that? "
I have no idea what your data consist of, nor what code you ran, nor what you expect the output to be like.
"Using the command S(2).A still leads to the right outcome. But why does matlab save it incorrectly?"
To "save" means to export some data from memory to a disk or drive. But you do not show how you are saving the data to file, so I cannot guess why it is saving "incorrectly".


その他の回答 (0 件)


Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by