I have a class that stores a figure handle. With the new Matlab handle graphics hg2 I get a "handle to deleted figure" error.
classdef mytestclass
properties
hFig = figure
end
end
Creating just one instance of the class works fine, I get a.hFig as a valid figure handle.
a = mytestclass % this will open the figure
But when I close the figure, and create another instance of the class, I get
b = mytestclass % this won't open any figure
b.hFig % this is now a handle to a deleted figure
Am I doing something wrong with classes? Or is this a bug?
I tried your example on Matlab 2009a (long before the new HG2) and the behavior is strictly the same as you describe.
it seems you are doing something slightly wrong with the way
classes
work in Matlab.Basically you can assign the default value of a property with any type of numeric/text value:
but do not assign them with a handle to something
otherwise all the objects of your class (even newly created) will point to the same actual handle which was created only once when your first object was instantiated.
If you want to generate a different graphic object (figure) each time you create a new object of your class, you have to generate it in the class constructor.
So your class become:
from the help: