I have created a class-managed GUI in which the following methods are defined
function h = make_figure(this)
h = figure('CloseRequestFcn',@this.close_figure);
this.openFigures(end+1) = h;
end
function close_figure(this, src, event)
this.openFigures(this.openFigures == src) = [];
delete(src);
end
Throughout the life of the GUI maney figures are created and closed with the aid of the methods above. Now the weird part…
If I double click a figure file eg (myfigure.fig) in MATLAB’s “Current Folder” window and then try to close it (while the GUI is still alive) then the close_figure method of the GUI’s class runs although it wasn’t created with h = figure('CloseRequestFcn',@this.close_figure); of make_figure method!!! This is the first weird part. The second weird part is that, although an unintented part of code is executed, it shouldn’t create problems because the command this.openFigures(this.openFigures == src) = []; actually leaves this.openFigures unchanged and practically all that close_figure does is to delete(src) which is desirable. Despite the above, my program behaves… weird after the above happens.
I have noticed that if I hit figure in the command window and then close it, everything is OK, the close_figure method is not executed. The problem reveals itself only when opening a .fig file created by the GUI’s class.
Any ideas?
I just tried the scenario you described, and I am not seeing any weird behavior; create instance of the class, use it to make a figure, then open a FIG file from the “Current Folder” file browser, and close it..
Here is the class definition I used:
(Tested in R2012a on Windows XP 32-bit.)
EDIT
Ok I think I know what the problem is. Here are the step to reproduce. First add the following method to the class above:
Now using our class wrapper, lets create a figure, and save it to FIG file:
Next clear your workspace
clear all, and move the class definition fileMyFigureClass.mto some other folder (something not on the path). Now try to load the saved figurehgload fig01.fig, and it will throw the error:You will have to issue the following command to forcefully close it:
EDIT2
One solution (as mentioned in the comments), is to reset the figure close callback function just before saving it. Here is how I modified the
save_figurefunction above: