Class with dynamic properties but no handle

조회 수: 6 (최근 30일)
Alexandru Bitca
Alexandru Bitca 2019년 6월 11일
댓글: Alexandru Bitca 2019년 6월 12일
Hi,
This might seem like a stupid thing to ask but humour me:
I have a class (call it ParameterClass) that is a subclass of the dynamicprops handle class. Thus I can add properties dynamically which is what I want. ParameterClass is also a property of another object, call it testcase.
Because ParameterClass is a subclass of a handle class, it itself becomes a handle class.
Inside a GUI object I want to store multiple instances of the testcase object, thus multiple references to the ParameterClass, but I want the ParameterClass to stop being a handle class as I would like to be able to update a property of ParameterClass in a certain instance of a testcase and I don't want that change to be reflected in all other testcase objects.
So my question essentially is how do I go from a handle class to a value class? Is there another way of adding dynamic properties to a class without it becoming a handle class?
Hope this question makes sense.
Thank you for your help,
Alex.
  댓글 수: 2
Adam
Adam 2019년 6월 12일
may be useful, though I suspect that what Per says is what you have going on here. Your terminology is a little unclear as you talk about 'multiple references to the ParameterClass'. Whether a class derives from handle or not, you still create objects of the class to work with. If you create two objects of a handle-derived class independently then they remain totally independent of each other. If you take multiple copies of one object then you get reference copies that are not independent.
I haven't really used the matlab.mixin.copyable much because it doesn't really go far enough for my purposes usually. Instead I use a deep copy function that I think I picked up somewhere from an online search years ago:
function newObj = deepCopy( obj )
try
% R2010b or newer - directly in memory (faster)
objByteArray = getByteStreamFromArray(obj);
newObj = getArrayFromByteStream(objByteArray);
catch
% R2010a or earlier - serialize via temp file (slower)
fname = [tempname '.mat'];
save(fname, 'obj');
newObj = load(fname);
newObj = newObj.obj;
delete(fname);
end
end
I assume I got it from a forum post somewhere as I don't have it in my 3rd party folder where attributed code from File Exchange lives.
Alexandru Bitca
Alexandru Bitca 2019년 6월 12일
Thanks gents,
Your answers helped me refine my understanding of the problem I've encountered and the deep copy method suggested by Adam works great!
Cheers,
Alex.

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

채택된 답변

per isakson
per isakson 2019년 6월 12일
편집: per isakson 2019년 6월 12일
"like a stupid thing to ask" No that's definately a good question that deserves an answer.
"how do I go from a handle class to a value class?" You cannot do that with an instance of a handle class. And you don't need to.
I assume that you in the properties block of the class, testcase, have assigned an instance of ParameterClass as default value. If so, that's what causing the unwanted behavior.
The solution is to assign the default value from within the constructor of testcase. See

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by