Class with dynamic properties but no handle

10 views (last 30 days)
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 Comments
Adam
Adam on 12 Jun 2019
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 on 12 Jun 2019
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.

Sign in to comment.

Accepted Answer

per isakson
per isakson on 12 Jun 2019
Edited: per isakson on 12 Jun 2019
"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

More Answers (0)

Categories

Find more on Properties in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!