フィルターのクリア

Creating a new object from existing object but after excluding one specific property.

3 ビュー (過去 30 日間)
Hi All,
I want to copy an object from exsiting object but don't want to copy all the properties, more specifically one perticular property.
Problem i am facing is that i don't know all the perperties exisiting in the object from which i am copying. But if there is this one property in this object, i want to exclude it and copy rest of the object to the new object. For example i have obj1:
function obj1 = obj1
obj1.A = 10;
obj1.B = 20;
obj1.C = 30;
end
And now i copy this object to a new obj:
x = obj1
y = obj2(x)
function obj2 = obj2(x)
obj2 = x(x.*!=x.C)
end
In second part of code, line number 5 has syntax error on purpose to represent what i want. I

採用された回答

J. Alex Lee
J. Alex Lee 2020 年 8 月 11 日
If you actually mean structures, as your example suggests, are you aware of "isfield", which will check if a structure contains a particular field name?
If you actually mean objects/classes, then there is "isprop" to check if a particular property exists.
  4 件のコメント
Walter Roberson
Walter Roberson 2020 年 8 月 11 日
編集済み: Walter Roberson 2020 年 8 月 12 日
On some cases it might make sense to subclass from mixins and implement a copy method excluding particular properties.
Deepak Gupta
Deepak Gupta 2020 年 8 月 12 日
Thanks Alex and Walter, this is what i was looking for.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConstruct and Work with Object Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by