Structured Cell array assignment question

1 回表示 (過去 30 日間)
John Petersen
John Petersen 2016 年 1 月 28 日
回答済み: Walter Roberson 2016 年 1 月 29 日
If I have something like
A(1).a = {c};
A(2).a = {d};
and
B(1).a = {e};
B(2).a = {f};
and want to assign B.a to A.a,
for i=1:length(A)
A(i).a = B(i).a;
end
is there a way to do this without doing a 'for' loop?

採用された回答

Walter Roberson
Walter Roberson 2016 年 1 月 29 日
In the special case where a is the only field in A, but there are potentially additional fields in B, then
A = struct('a', {B.a});

その他の回答 (1 件)

John BG
John BG 2016 年 1 月 29 日
A.a and B.a are type struct. How do you expect to be able to assign B.a=A.a without making sure that the structures have same fields?
The obvious solution is that you build a function, kind of assignstruct(struc1,struc2) and that in this function you first make sure you can copy data from one structure to the other, and if struct1 struct2 are not compatible then return error.
John

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by