I need to compare two strctures and find the common between them and then keep the common fields as the new structure.

4 件のコメント

Geoff Hayes
Geoff Hayes 2019 年 3 月 10 日
Praveen - please clarify what you mean by the common between them. Do the structures have fields that you want to compare? Or something else? Please provide a small example.
Praveen Kumar Pakkirisamy
Praveen Kumar Pakkirisamy 2019 年 3 月 10 日
For example
structure 1='field1','field2','field3';
structure 2='field1','field4','field3';
The common between them is field1 and field 3 and i want to write that in structure 1.
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 3 月 10 日
structure_1={'field1','field2','field3'};
structure_2={'field1','field4','field3'};
structure_1=intersect(structure_1,structure_2)
madhan ravi
madhan ravi 2019 年 3 月 10 日
Sir Walter has given the answer already before 24mins..

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

 採用された回答

Walter Roberson
Walter Roberson 2019 年 3 月 10 日

0 投票

common_fields = intersect( fieldnames(structure1), fieldnames(structure2));
for fn = common_fields
this = fn{1};
new_struct(1).(this) = structure1.(this);
new_struct(2).(this) = structure2.(this);
end

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Structures についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by