How to copy an object that is not a graphic one?

1 回表示 (過去 30 日間)
JFz
JFz 2018 年 4 月 13 日
回答済み: Steven Lord 2018 年 4 月 13 日
Hi,
I have an object of a class which I defined. I would like to make a copy of that object. But copy or copyobj do not seem to work. What should I do?
Thanks!
  1 件のコメント
JFz
JFz 2018 年 4 月 13 日
BTW, the object is derived from handle.

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

採用された回答

Matt J
Matt J 2018 年 4 月 13 日
編集済み: Matt J 2018 年 4 月 13 日
In general, you would have to write a copy() method specific to the needs of your class. The copy() method would have to call the class constructor with suitable arguments to generate a copy.
If your class is sufficiently simple that a copy can be made just by dot-indexing all of the non-Dependent, public property values and writing them to a second object,
objcopy.prop1 = obj.prop1;
objcopy.prop2 = obj.prop2;
etc...
then you can just use my attached copyprops() utility, which automates this.
  1 件のコメント
JFz
JFz 2018 年 4 月 13 日
I see. Will try it. Thanks.

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2018 年 4 月 13 日
Consider having your class subclass the matlab.mixin.Copyable class. It provides a copy method and a copyElement method that you can use to customize the copy behavior. For more information see the topic "Implement Copy for Handle Classes" in the documentation.

カテゴリ

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