MATLAB: How to store a reference to an existing figure without opening new figures

MATLAB

I want my application to be able to identify and access figures which it previously created and are still open.
With previous versions (before R2014b) I was able to accomplish this with this simple workflow:
  • application creates figures
  • application saves handles (double values) of all figures it created
  • application quits, figures remain open!
  • application starts, retrieves the handles from preferences (no figures are created!)
  • application does whatever it wants with the figures it created
How can I do that with newer versions of MATLAB?

Best Answer

  • To accomplish this, you can now use the 'Number' property of a figure, rather than the handle object returned by 'gcf':
    Each figure window has an associated number (generally based on order of creation). When saving, you can simply save the "Number" property of each figure:
    myFigureHandle = gcf;
    figureNumber = myFigureHandle.Number;
    save test figureNumber
    clear
    load test
    % Return handle for specific figure
    myFigureHandle = figure( figureNumber );
    This will allow you to keep an association with each open figure window, and the saved variable is only a double (rather than a true handle) so no figure will be created upon load.