Class with dynamic properties but no handle
古いコメントを表示
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
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
2019 年 6 月 12 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Creating, Deleting, and Querying Graphics Objects についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!