フィルターのクリア

Is it possible to concatenate sub-structures within structures into single variables?

3 ビュー (過去 30 日間)
I am working with multiple structures that contain several sub-structures, each containing numerous fields. The sub-structures and fields all share the same naming convention for each of the separate "top-level" structures. For example;
Struct1.A.B.data = [5000 x 1 single];
Struct2.A.B.data = [3000 x 1 single];
I'd like to append the variables for each of the subsequent structures into the first one without creating additional structures. For example, after appending all of the additional data, structure 1 would become;
Struct1.A.B.data = [8000 x 1 single] instead of Struct1.A.B.data = [2 x 1 struct]
I'm currently doing this manually by concatenating each field individually and it works but requires many lines of code for large structures, see below.
Struct1.A.B.data = [Struct1.A.B.data Struct2.A.B.data];
This repeats for sub-structures A through Z and sub-structures B through Z, which is very tedious.
Is there a better way of acheiving this?
Side note, I tried using the method described in the question below but this creates the additional structures mentioned above instead of adding the data directly to the variables.

採用された回答

Matt J
Matt J 2024 年 1 月 9 日
編集済み: Matt J 2024 年 1 月 9 日
A double nested loop could be used, e.g.,
for A=string(fieldnames(Struct1))'
for B=string(fieldnames(Struct1.(A)))'
Struct1.(A).(B).data = [Struct1.(A).(B).data Struct2.(A).(B).data];
end
end
  3 件のコメント
Matt J
Matt J 2024 年 1 月 10 日
編集済み: Matt J 2024 年 1 月 10 日
"Dot indexing is not supported for variables of this type."...Any ideas on what is causing this error?
Yes, but there is no reason to be guessing. You should trap the error with dbstop and see exactly what the variable type is.
The solution assumes all fields of Struct1 and Struct2 are themselves structs. If that is violated, it would explain the error.
Jack Smith
Jack Smith 2024 年 1 月 10 日
I forgot that the sub-structures A and B contained some variables that were not structures, such as strings. I've added an if statement to the inner loop to skip these cases which has fixed the problem.
Thanks for all your help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeVariables についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by