필터 지우기
필터 지우기

Retrieving/storing the state of a handle

조회 수: 2 (최근 30일)
Patrick Mboma
Patrick Mboma 2016년 10월 20일
댓글: Steven Lord 2016년 10월 20일
Hi,
Is it possible to retrieve the old state of a modified handle object? more concretely, suppose a have a class such as
classdef myclass < handle
...
end
and I create an instance of myclass, e.g. h0=myclass(varargin) and create a copy h=h0; I understand that if I modify h0, then h will also be modified. My problem is that I would like to pass h0 through a function that modifies it randomly and I need to repeat the exercise several times and see how the results differ. In order to be able to do that, I need the original state h.
Is there anyway to achieve this?
Thanks,

채택된 답변

Steven Lord
Steven Lord 2016년 10월 20일
You could make your class a value class rather than a handle class or you could make your class Copyable. By subclassing matlab.mixin.Copyable the class inherits a copy method that:
  • Copies data without calling class constructors or property set functions and therefore produces no side effects.
  • Enables subclasses to customize the copy behavior
When you need to make random changes to the object that won't affect the "main" instance of your object, pass a copy. When you want to have two instances that have normal handle semantics (changing one changes the other) use non-indexed assignment to create the second instance.
  댓글 수: 2
Patrick Mboma
Patrick Mboma 2016년 10월 20일
matlab.mixin.Copyable seems to be what I am after.
Value classes take up lots of memory or am I wrong? Is there any advantage of value classes over handle/Copyable classes?
Thanks
Steven Lord
Steven Lord 2016년 10월 20일
Value classes can take up lots of memory. So can handle classes. It all depends on what is stored in their properties and how you use them.
You might find this page and/or this page from the documentation informative in learning the differences between the different semantics and when you might want to choose one over the other.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming Utilities에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by