MATLAB: How to reopen a closed figure with a pre-allocated figure handle from Matlab 2014b onwards


I have pre-allocated my figures such that I generate an empty figure window without a plot which is assigned to a figure handle at the start of a script. I then close these figures as I don't require they all be opened or remain open all at once. I want to reopen these figures one by one at appropriate times, but it appears that upon closing a figure by its assigned/allocated handle within Matlab 2014b and onwards, the assigned figure handle empties itself of all information related to the figure. I understand that the nature of the figure handle has changed to an object now and I need to utilise the figure.Number property to access it's assigned integer.
h = figure; % Pre-allocate figure handle. Generates figure object in Matlab 2014b and onwards.
close(h); % Close pre-allocated figure.
figure(h.Number); % Re-open figure.
This produces the following error.
Invalid or deleted object.
How do I reopen a closed figure in Matlab 2014b and onwards?

Best Answer

  • h.Number becomes invalid once you close the figure. You would need to record the number before closing.
    In all versions of MATLAB including before R2014b, there was no such thing as reopening a figure: there was just creating a new figure that had the same figure number. When you close a figure the behavior has always been that the associated structure is destroyed.
    If you want to reopen a figure then you should be thinking in terms of making the figure visible off while you do not need the figure showing, and then adjusting properties and setting visible on when you need the figure.