How do I combine a structure array into one structure

19 ビュー (過去 30 日間)
Shelley Snider
Shelley Snider 2021 年 6 月 8 日
回答済み: James Tursa 2021 年 6 月 8 日
I have a structure array where each field of the same name is the same size. I want to combine each field over the array into one field. For example:
A(1).x = [1 2 3];
A(1).y = [4 5 6];
A(2).x = [7 8 9];
A(2).y = [10 11 12];
I want the new structure to be
B.x = [1 2 3;
7 8 9];
B.y = [4 5 6;
10 11 12];
These could also be a cell of structures if that makes things easier.

採用された回答

Walter Roberson
Walter Roberson 2021 年 6 月 8 日
A(1).x = [1 2 3];
A(1).y = [4 5 6];
A(2).x = [7 8 9];
A(2).y = [10 11 12];
B.x = vertcat(A.x);
B.y = vertcat(A.y);
B
B = struct with fields:
x: [2×3 double] y: [2×3 double]

その他の回答 (1 件)

James Tursa
James Tursa 2021 年 6 月 8 日
F = fieldnames(A);
n = numel(F);
C = arrayfun(@(i)vertcat(A.(F{i})),1:n,'uni',false);
FC = [F';C];
B = struct(FC{:});

カテゴリ

Help Center および File ExchangeFPGA, ASIC, and SoC Development についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by