make saved figures visible on
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
Adam
on 16 Dec 2016
Are these figures to be loaded by other people using Matlab? If not (or even if they are) then I would suggest just writing a simple wrapper function for loading a figure which sets its 'Visible' property to 'on' and then you just call your function instead of a call to the raw figure loading.
Heinz
on 16 Dec 2016
Adam
on 16 Dec 2016
I never save figures so I'm not too familiar with how they save and what you can manipulate. I always save data and recreate the figure from that when I need.
Adam
on 16 Dec 2016
Can't you ship the wrapper loading function to them with the figures? I tried a few attempts at doing this, but it seems the figure is just literally saved exactly as you see it and loading it as a Mat file and setting it to 'Visible' will make it visible for you too. IT would be the same problem if you did the other 'invisibility' trick of creating the figure off-screen because then it would also be off-screen when loaded.
Heinz
on 16 Dec 2016
Adam
on 16 Dec 2016
Since that is your solution why not just do it when you first create the figure (or before you save it), then you don't need to load the file as a .mat and manipulate that structure at all. This solution won't suddenly cause it to appear visible on your screen like others would.
Heinz
on 16 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
More Answers (2)
Jan
on 16 Dec 2016
0 votes
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
0 votes
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.
1 Comment
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!