4

I have the same question asked here: Save Matlab figure without plotting it?

But the problem with the solution given there is that I can't open the saved figures in visible state with doubleclick afterwards. Looks like the savefig command saves the visible state. Same with saveas.

h=figure;
set(h,'Visible','off');
savefig('TestExample.fig');

b=openfig('TestExample.fig');

With this command I can see the figure, but I simply want to doubleclick and see it:

set(b,'Visible','on');
TAK
  • 113
  • 1
  • 7
  • Unless you want to write binary into the fig file, I'm not sure how you can save the state of some graphic handle without getting that state back on load. The only things that might be different are the handles themselves and the position of the figure... Wouldn't setting the figure visible just for the time of saving be okay? Or moving it away from the screen? –  Nov 26 '15 at 16:25
  • @CST-Link `openfig` has a specific option to set the `'Visible'` property on load. More properties can be changed by defining the `'ResizeFcn'` to do the work, as I discovered reading some MATLAB central threads! Details in answer below. – mikkola Nov 26 '15 at 16:46

2 Answers2

3

The best solution for me is (thanks for links, How to edit property of figure saved in .fig file without displaying it):

figure('Visible','off')
set(gcf,'Visible','off','CreateFcn','set(gcf,''Visible'',''on'')')
savefig('Test.fig')
close

Figures don't pop up and I can open them visible with doubleclick only.

TAK
  • 113
  • 1
  • 7
2

The documentation seems to shed some light on the issue:

Create a surface plot and make the figure invisible. Then, save the figure as a MATLAB figure file. Close the invisible figure.

surf(peaks)
set(gcf,'Visible','off')
savefig('MySavedPlot.fig')
close(gcf)

Open the saved figure and make it visible on the screen.

openfig('MySavedPlot.fig','visible')

...however, sadly it probably will not work when you use the double-clicking interface. The issue has been discussed also here, and would require changing the default behaviour of openfig. It is possible by editing the built-in function, but kind of dirty.

Another workaround solution is suggested in the comments further down by Jesse Hopkins:

Set the ResizeFcn on the figure to renable visibility. According to Matlab documentation, and in practice, ResizeFcn is called when figure is created: set(h,'ResizeFcn','set(gcf,''visible'',''on'')');

The nice thing is, this workaround should work for setting on-load any property you might want to set on the figure handle being loaded.

mikkola
  • 3,376
  • 1
  • 19
  • 41