make saved figures visible on
25 views (last 30 days)
Show older comments
I perform large batches of simulations, in which I generate a lot of figures and save them as fig. To avoid, that these figures pop up and make any further work on the machine impossible, I generate these plots in invisible figures. Then i save them as .fig files. This action conserves the property invisible, so that a loading of such a figure only leads to a visible figure, after doing a set(gcf,'visible','on')
I tried to set the state of the visibility in the file on the disk by using the following function:
function makevisible(file)
f=load(file,'-mat');
n=fieldnames(f);
f.(n{1}).properties.Visible='on'; % this line does not have much effect in 16b, used to be the key line in earlier versions.
f.(n{2}).GraphicsObjects.Format3Data.Visible='on';
save(file,'-struct','f')
end
sadly the command: f=load(file,'-mat'); does not only load the data into the workspace struct f, but also generates a figure. This figure then becomes visible, when changing the state. I tried to delete this figure with
close gcf
even before I set the visibility. But this does not only delete the figure, but also alters the data in f. after the close gcf command f is the struct of a "having been deleted figure".
How can I achieve to have saved figure files with visible on, without having every one of these figures to pop up on the screen. ???
My Solution
Its not exactly what I originally wanted, as it still saves the figures invisible. But it solves the described problem by defining a create function, which sets the visibility to on during creation.
function makevisible(file)
top =load(file,'-mat' ,'hgM_070000' );
top.hgM_070000.GraphicsObjects.Format3Data.CreateFcn = 'set(gcf,''visible'',''on'')'
save(file,'-struct','top','-append')
end
8 Comments
Jan
on 17 Dec 2016
Edited: Jan
on 17 Dec 2016
Creating callbacks as strings remains dangerous: As soon as a customer has redefined "gcf" in the command window, opening you fig files will produce strange and confusing results.
In older Matlab versions LOAD of a fig file did not open the figure. That this is the case now is rather confusing and impractical.
Accepted Answer
Heinz
on 19 Dec 2016
2 Comments
Adam
on 19 Dec 2016
You can be a little more robust by using gcbo rather than gcf as this is explicitly the handle to the object whose callback is currently executing rather than just the current figure. This function handle approach for the CreateFcn appears to work after a quick test:
@(src,evt)set(gcbo,'visible','on')
Lucademicus
on 23 Oct 2019
Awesome solution Adam, thank you, this works like a charm on R2018b and R2019a.
More Answers (2)
Jan
on 16 Dec 2016
Are you really sure, that f=load(file,'-mat') opens the figure? This would be very strange.
Is it required to set "f.(n{2}).GraphicsObjects.Format3Data.Visible='on'"? Which Matlab version are you using?
A simply solution would to insert the code to enable the visibility during the loading of the figure.
2 Comments
Jan
on 16 Dec 2016
When loading the figure file as a struct opens the figure already, this figure must contain any code, which triggers the displaying. What a pitty. This seems to be a perfect example for the bad idea to mix code and data. Blame Mathworks.
Then you could go the hard way: Create the figures invisible at first. Then open all fig files, set the visibility and save the fig fiels again. Although this pollutes your screen, it will do this for a short period of time only.
In older Matlab versions the conversion from a figure to code worked: http://www.mathworks.com/matlabcentral/fileexchange/20152-guidegetter . I'm not sure, if this could be useful for your case.
Steven Lord
on 16 Dec 2016
Would using a command to open the figure that forces it to be visible satisfy your needs? If so use the openfig function with the 'visible' value for the visibility input argument as per the second example on that documentation page.
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!