Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
save figures without displaying

Subject: save figures without displaying

From: Oliver

Date: 14 Apr, 2011 10:52:04

Message: 1 of 19

Hi all, I've looked around for ages but cannot find a different solution. I fear there isn't one.

Problem: I have a tool that batch processes data and creates loads (00's) of figures. I don't want them flashing up and getting in the way while the user is still trying to work on the machine.

Current solution:
set(fig, 'visible','off')
saveas(fig,'file.fig','fig')
close(fig)

This is fine, however when trying to open the saved .fig file they remain invisible.

I tried....
set(fig, 'visible','off')
saveas(fig,'file.fig','fig')
set(fig, 'visible','on')
close(fig)

In the hope it wouldn't be displayed, but it is.

Next idea is to open the .fig using fopen and edit the file manually to set visibility to on, then re-save the edited .fig, but the data format is unknown.

Any ideas how to save invisible figures, but have them open as visible?

Subject: save figures without displaying

From: Grzegorz Knor

Date: 14 Apr, 2011 11:39:04

Message: 2 of 19

"Oliver" wrote in message <io6jkk$5em$1@fred.mathworks.com>...
> Hi all, I've looked around for ages but cannot find a different solution. I fear there isn't one.
>
> Problem: I have a tool that batch processes data and creates loads (00's) of figures. I don't want them flashing up and getting in the way while the user is still trying to work on the machine.
>
> Current solution:
> set(fig, 'visible','off')
> saveas(fig,'file.fig','fig')
> close(fig)
>
> This is fine, however when trying to open the saved .fig file they remain invisible.
>
> I tried....
> set(fig, 'visible','off')
> saveas(fig,'file.fig','fig')
> set(fig, 'visible','on')
> close(fig)
>
> In the hope it wouldn't be displayed, but it is.
>
> Next idea is to open the .fig using fopen and edit the file manually to set visibility to on, then re-save the edited .fig, but the data format is unknown.
>
> Any ideas how to save invisible figures, but have them open as visible?

Try this code:

figure('Visible','off')
plot(1:100)
saveas(gcf,'file.fig','fig')

openfig('file.fig','new','visible')

best regards
Grzegorz

Subject: save figures without displaying

From: Oliver

Date: 14 Apr, 2011 12:24:05

Message: 3 of 19

Thanks, but I'm trying to make it so the user doesn't have to enter any code. They should just be able to double click the .fig file and open it from explorer.

I think the only way is to make it so the fig is saved with visible 'on', but somehow not actually display the figure when visible is set to 'on'...

Any ideas about minimising figure windows?

Subject: save figures without displaying

From: John

Date: 14 Apr, 2011 12:57:20

Message: 4 of 19

what about positioning the figures out of the screen? just use normalized units and minus values for x and y positions. whenever you need them, you can move them to the normal positions.

Subject: save figures without displaying

From: Grzegorz Knor

Date: 14 Apr, 2011 13:01:21

Message: 5 of 19

You can import figure to workspace with importdata command:
a = importdata('file.fig')
And then you could change it properties:
a.properties.Visible = 'on'

But I have no idea how to save it again in fig format.

Grzegorz

Subject: save figures without displaying

From: Yuri Geshelin

Date: 14 Apr, 2011 13:34:08

Message: 6 of 19

Oliver,

I think the workaround is to set up a listener, which will monitor the children of the root and as soon as the new ones appear make them visible. Unless it contradicts with your other preferences and work style, you can add the listener to startup: it won't prevent you from setting the 'visible' property to 'off', as it will only be applied to the new figure handles that appear in the workspace.

Defining Events and Listeners:

http://www.mathworks.com/help/techdoc/matlab_oop/brb6gnc.html#top_of_page

HTH

Yuri

"Oliver" wrote in message <io6jkk$5em$1@fred.mathworks.com>...
> Hi all, I've looked around for ages but cannot find a different solution. I fear there isn't one.
>
> Problem: I have a tool that batch processes data and creates loads (00's) of figures. I don't want them flashing up and getting in the way while the user is still trying to work on the machine.
>
> Current solution:
> set(fig, 'visible','off')
> saveas(fig,'file.fig','fig')
> close(fig)
>
> This is fine, however when trying to open the saved .fig file they remain invisible.
>
> I tried....
> set(fig, 'visible','off')
> saveas(fig,'file.fig','fig')
> set(fig, 'visible','on')
> close(fig)
>
> In the hope it wouldn't be displayed, but it is.
>
> Next idea is to open the .fig using fopen and edit the file manually to set visibility to on, then re-save the edited .fig, but the data format is unknown.
>
> Any ideas how to save invisible figures, but have them open as visible?

Subject: save figures without displaying

From: Oliver Woodford

Date: 19 Apr, 2011 08:56:04

Message: 7 of 19

"Oliver" wrote:
> Problem: I have a tool that batch processes data and creates loads (00's) of figures. I don't want them flashing up and getting in the way while the user is still trying to work on the machine.

There is a much simpler solution than making the figures not visible. Always render into the same figure:

for a = 1:num_figs
   set(0, 'CurrentFigure', 1);
   clf reset;
   % Plot here
   % Save here, using print or export_fig
end

This avoids bringing the figure to the foreground every time it's rendered.

Subject: save figures without displaying

From: Yuri Geshelin

Date: 21 Apr, 2011 01:07:05

Message: 8 of 19

"Oliver" wrote in message <io6jkk$5em$1@fred.mathworks.com>...
> Hi all, I've looked around for ages but cannot find a different solution. I fear there isn't one.
>
> Problem: I have a tool that batch processes data and creates loads (00's) of figures. I don't want them flashing up and getting in the way while the user is still trying to work on the machine.
>
> Current solution:
> set(fig, 'visible','off')
> saveas(fig,'file.fig','fig')
> close(fig)
>
> This is fine, however when trying to open the saved .fig file they remain invisible.
>
> I tried....
> set(fig, 'visible','off')
> saveas(fig,'file.fig','fig')
> set(fig, 'visible','on')
> close(fig)
>
> In the hope it wouldn't be displayed, but it is.
>
> Next idea is to open the .fig using fopen and edit the file manually to set visibility to on, then re-save the edited .fig, but the data format is unknown.
>
> Any ideas how to save invisible figures, but have them open as visible?

While seeking for a solution to my problem, I found a very easy solution to yours. It is much easier than the listener that I proposed.

A clue:

>> help open
 
. . . . . . . . . . . . . . . . . . . . .
 
OPEN is user-extensible. To open a file with the extension ".XXX",
    OPEN calls the helper function OPENXXX, that is, a function
    named 'OPEN', with the file extension appended.
. . . . . . . . . . . . . . . . . . . . .

The remedy:

1) Locate function OPENFIG and copy it to a directory, which is found on the path before the one, in which OPENFIG resides.
2) On the top of the code, find this line

      visible = ''

     (it goes after if nargin < 3)

      and replace it with

     visible = 'visible'

That’s it! :)

HTH

PS The solution by the other Oliver (to render all plots in the same figure) may not come in handy, because

(a) sometimes it is not easy to change the algorithm: the figure number may be a result of not trivial computation;
(b) a single figure will be seen all the time, while the objective is to get rid of all figures.

Subject: save figures without displaying

From: Lisa Thierbach

Date: 30 Oct, 2012 04:39:08

Message: 9 of 19

"Yuri Geshelin" wrote in message <ionvvp$j8a$1@fred.mathworks.com>...
> "Oliver" wrote in message <io6jkk$5em$1@fred.mathworks.com>...
> > Hi all, I've looked around for ages but cannot find a different solution. I fear there isn't one.
> >
> > Problem: I have a tool that batch processes data and creates loads (00's) of figures. I don't want them flashing up and getting in the way while the user is still trying to work on the machine.
> >
> > Current solution:
> > set(fig, 'visible','off')
> > saveas(fig,'file.fig','fig')
> > close(fig)
> >
> > This is fine, however when trying to open the saved .fig file they remain invisible.
> >
> > I tried....
> > set(fig, 'visible','off')
> > saveas(fig,'file.fig','fig')
> > set(fig, 'visible','on')
> > close(fig)
> >
> > In the hope it wouldn't be displayed, but it is.
> >
> > Next idea is to open the .fig using fopen and edit the file manually to set visibility to on, then re-save the edited .fig, but the data format is unknown.
> >
> > Any ideas how to save invisible figures, but have them open as visible?
>
> While seeking for a solution to my problem, I found a very easy solution to yours. It is much easier than the listener that I proposed.
>
> A clue:
>
> >> help open
>
> . . . . . . . . . . . . . . . . . . . . .
>
> OPEN is user-extensible. To open a file with the extension ".XXX",
> OPEN calls the helper function OPENXXX, that is, a function
> named 'OPEN', with the file extension appended.
> . . . . . . . . . . . . . . . . . . . . .
>
> The remedy:
>
> 1) Locate function OPENFIG and copy it to a directory, which is found on the path before the one, in which OPENFIG resides.
> 2) On the top of the code, find this line
>
> visible = ''
>
> (it goes after if nargin < 3)
>
> and replace it with
>
> visible = 'visible'
>
> That’s it! :)
>
> HTH
>
> PS The solution by the other Oliver (to render all plots in the same figure) may not come in handy, because
>
> (a) sometimes it is not easy to change the algorithm: the figure number may be a result of not trivial computation;
> (b) a single figure will be seen all the time, while the objective is to get rid of all figures.

Yiri:

Thank you! That worked like a charm.

Subject: save figures without displaying

From: Tor Inge Birkenes

Date: 13 Dec, 2012 12:21:08

Message: 10 of 19

> Any ideas how to save invisible figures, but have them open as visible?

I have made a function that changes the visible parameter on a saved .fig file, thus make a fig file saved invisible open as visible. It looks like this:

function makevisible(file)
f=load(file,'-mat');
n=fieldnames(f);
f.(n{1}).properties.Visible='on';
save(file,'-struct','f')
end

Just save this to makevisible.m and call makevisible('figure.fig'). Note that the .fig extension is necessary.

Sources:
http://undocumentedmatlab.com/blog/fig-files-format/ (to understand enough of the fig file format)
http://blogs.mathworks.com/pick/2008/08/20/advanced-matlab-dynamic-field-names/ (to make the code more efficient)

Subject: save figures without displaying

From: Steven_Lord

Date: 13 Dec, 2012 14:43:18

Message: 11 of 19



"Tor Inge Birkenes" <tor.inge.birkenes.remove.this@kongsberg.com> wrote in
message news:kach7k$84p$1@newscl01ah.mathworks.com...
>> Any ideas how to save invisible figures, but have them open as visible?
>
> I have made a function that changes the visible parameter on a saved .fig
> file, thus make a fig file saved invisible open as visible. It looks like
> this:

If this figure file is a regular figure, why not just open it up using
OPENFIG with the 'visible' flag?

http://www.mathworks.com/help/matlab/ref/openfig.html

If this is a figure file associated with a GUIDE GUI, see the Tips section
of that page for an alternative.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: save figures without displaying

From: Yuri Geshelin

Date: 13 Dec, 2012 22:29:08

Message: 12 of 19

"Tor Inge Birkenes" wrote in message <kach7k$84p$1@newscl01ah.mathworks.com>...
> > Any ideas how to save invisible figures, but have them open as visible?
>
> I have made a function that changes the visible parameter on a saved .fig file, thus make a fig file saved invisible open as visible. It looks like this:
>
> function makevisible(file)
> f=load(file,'-mat');
> n=fieldnames(f);
> f.(n{1}).properties.Visible='on';
> save(file,'-struct','f')
> end
>
> Just save this to makevisible.m and call makevisible('figure.fig'). Note that the .fig extension is necessary.
>
> Sources:
> http://undocumentedmatlab.com/blog/fig-files-format/ (to understand enough of the fig file format)
> http://blogs.mathworks.com/pick/2008/08/20/advanced-matlab-dynamic-field-names/ (to make the code more efficient)

Someone has already suggested a code in this thread, but this is not the solution Oliver was seeking. He wanted to open the files by clicking on them in explorer. This inevitably envokes OPENFIG. See the solution I suggested, it works.

Subject: save figures without displaying

From: Yuri Geshelin

Date: 13 Dec, 2012 22:35:11

Message: 13 of 19

"Steven_Lord" <slord@mathworks.com> wrote in message <kacpi6$8g9$1@newscl01ah.mathworks.com>...
>
>
> "Tor Inge Birkenes" <tor.inge.birkenes.remove.this@kongsberg.com> wrote in
> message news:kach7k$84p$1@newscl01ah.mathworks.com...
> >> Any ideas how to save invisible figures, but have them open as visible?
> >
> > I have made a function that changes the visible parameter on a saved .fig
> > file, thus make a fig file saved invisible open as visible. It looks like
> > this:
>
> If this figure file is a regular figure, why not just open it up using
> OPENFIG with the 'visible' flag?
>
> http://www.mathworks.com/help/matlab/ref/openfig.html
>
> If this is a figure file associated with a GUIDE GUI, see the Tips section
> of that page for an alternative.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

I would reiterate what I just replied to Tor Inge Birkenes. But maybe you know a workaround which allows to configurate OPENFIG, i.e. to change its default action?

Subject: save figures without displaying

From: Tor Inge Birkenes

Date: 14 Dec, 2012 08:07:08

Message: 14 of 19

"Yuri Geshelin" wrote in message <kadl6v$qg7$1@newscl01ah.mathworks.com>...
> "Steven_Lord" <slord@mathworks.com> wrote in message <kacpi6$8g9$1@newscl01ah.mathworks.com>...
> >
> >
> > "Tor Inge Birkenes" <tor.inge.birkenes.remove.this@kongsberg.com> wrote in
> > message news:kach7k$84p$1@newscl01ah.mathworks.com...
> > >> Any ideas how to save invisible figures, but have them open as visible?
> > >
> > > I have made a function that changes the visible parameter on a saved .fig
> > > file, thus make a fig file saved invisible open as visible. It looks like
> > > this:
> >
> > If this figure file is a regular figure, why not just open it up using
> > OPENFIG with the 'visible' flag?
> >
> > http://www.mathworks.com/help/matlab/ref/openfig.html
> >
> > If this is a figure file associated with a GUIDE GUI, see the Tips section
> > of that page for an alternative.
> >
> > --
> > Steve Lord
> > slord@mathworks.com
> > To contact Technical Support use the Contact Us link on
> > http://www.mathworks.com
>
> I would reiterate what I just replied to Tor Inge Birkenes. But maybe you know a workaround which allows to configurate OPENFIG, i.e. to change its default action?

Hi!
Openfig would as you say open the figure visible, but as Yuri pointed out this is not a solution to the original question. Yuris solution works by changing the default of openfig, i do not know any other way of doing that.

However, I do not like to edit builtin functions. It may cause trouble with later updates and require configuration of the computer opening the fig-files. Therefore I found a solution that did not require any configuration of the computer opening the .fig file.
This is necessary for me because my figures must open visible on other peoples computer. My fuction imports the data of the figure, edits the appropiate visibility parameter and saves it to the same .fig file. Therefore the figure will open visible on all computers.

The drawback of my solution is that it requires an extra import export step. I think this can be avoided by using the handle2struct function, but I do not have the time to look into it now. You should compare mine and Yuris solution to see wich works best for you.

Regards
Tor Inge Birkenes

Subject: save figures without displaying

From: Jesse Hopkins

Date: 1 Nov, 2013 22:23:08

Message: 15 of 19

This thread has been dead for a while, but I have found a simple solution to this that hasn't been mentioned here. 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'')');

Subject: save figures without displaying

From: Bo Zhang

Date: 22 Nov, 2013 16:40:22

Message: 16 of 19

"Oliver" wrote in message <io6jkk$5em$1@fred.mathworks.com>...
> Hi all, I've looked around for ages but cannot find a different solution. I fear there isn't one.
>
> Problem: I have a tool that batch processes data and creates loads (00's) of figures. I don't want them flashing up and getting in the way while the user is still trying to work on the machine.
>
> Current solution:
> set(fig, 'visible','off')
> saveas(fig,'file.fig','fig')
> close(fig)
>
> This is fine, however when trying to open the saved .fig file they remain invisible.
>
> I tried....
> set(fig, 'visible','off')
> saveas(fig,'file.fig','fig')
> set(fig, 'visible','on')
> close(fig)
>
> In the hope it wouldn't be displayed, but it is.
>
> Next idea is to open the .fig using fopen and edit the file manually to set visibility to on, then re-save the edited .fig, but the data format is unknown.
>
> Any ideas how to save invisible figures, but have them open as visible?

You may want to try this:
h=openfig('yourfig.fig','new','visible');
saveas(h,'yourfig.fig');
close(h);
By using new you creat a copy of the original figure, set the copy to be visible, and then overwrite the invisible original figure.

Subject: save figures without displaying

From: Benjamin

Date: 11 Feb, 2014 18:14:10

Message: 17 of 19

"Oliver" wrote in message <io6jkk$5em$1@fred.mathworks.com>...
> Hi all, I've looked around for ages but cannot find a different solution. I fear there isn't one.
>
> Problem: I have a tool that batch processes data and creates loads (00's) of figures. I don't want them flashing up and getting in the way while the user is still trying to work on the machine.
>
> Current solution:
> set(fig, 'visible','off')
> saveas(fig,'file.fig','fig')
> close(fig)
>
> This is fine, however when trying to open the saved .fig file they remain invisible.
>
> I tried....
> set(fig, 'visible','off')
> saveas(fig,'file.fig','fig')
> set(fig, 'visible','on')
> close(fig)
>
> In the hope it wouldn't be displayed, but it is.
>
> Next idea is to open the .fig using fopen and edit the file manually to set visibility to on, then re-save the edited .fig, but the data format is unknown.
>
> Any ideas how to save invisible figures, but have them open as visible?

Jesse, you're solution is my favorite so far and I will be using it moving forward, but I have another for people who don't want to mess with the ResizeFcn.

On my system, if I create the figure with visible off:

fh = figure(...,'Visible','off');

then build the content while the figure is invisible, I can then do the following:

set(fh,'Visible','on');
saveas(fh,'name','fig');
delete(fh);

This results in the figure being saved with visible enabled but the figure never shows up on my screen or takes focus away from whatever else I'm working on. I imagine this is because the figure is deleted before it can be fully rendered so it might not work on slower systems or when MATLAB is running multiple things simultaneously.

If you're comfortable with setting/editing the ResizeFcn, that's a more robust solution.

Subject: save figures without displaying

From: Joe

Date: 29 Aug, 2014 18:10:09

Message: 18 of 19

"Benjamin Avants" wrote in message <lddp9i$r70$1@newscl01ah.mathworks.com>...
> "Oliver" wrote in message <io6jkk$5em$1@fred.mathworks.com>...
> > Hi all, I've looked around for ages but cannot find a different solution. I fear there isn't one.
> >
> > Problem: I have a tool that batch processes data and creates loads (00's) of figures. I don't want them flashing up and getting in the way while the user is still trying to work on the machine.
> >
> > Current solution:
> > set(fig, 'visible','off')
> > saveas(fig,'file.fig','fig')
> > close(fig)
> >
> > This is fine, however when trying to open the saved .fig file they remain invisible.
> >
> > I tried....
> > set(fig, 'visible','off')
> > saveas(fig,'file.fig','fig')
> > set(fig, 'visible','on')
> > close(fig)
> >
> > In the hope it wouldn't be displayed, but it is.
> >
> > Next idea is to open the .fig using fopen and edit the file manually to set visibility to on, then re-save the edited .fig, but the data format is unknown.
> >
> > Any ideas how to save invisible figures, but have them open as visible?
>
> Jesse, you're solution is my favorite so far and I will be using it moving forward, but I have another for people who don't want to mess with the ResizeFcn.
>
> On my system, if I create the figure with visible off:
>
> fh = figure(...,'Visible','off');
>
> then build the content while the figure is invisible, I can then do the following:
>
> set(fh,'Visible','on');
> saveas(fh,'name','fig');
> delete(fh);
>
> This results in the figure being saved with visible enabled but the figure never shows up on my screen or takes focus away from whatever else I'm working on. I imagine this is because the figure is deleted before it can be fully rendered so it might not work on slower systems or when MATLAB is running multiple things simultaneously.
>
> If you're comfortable with setting/editing the ResizeFcn, that's a more robust solution.

Jessie's

figure2 = figure
set(figure2, 'visible','off')
set(figure2,'ResizeFcn','set(gcf,''visible'',''on'')');
saveas(figure2,'name','fig');

worked for me too.

I had one thing that didn't work though. One of my figures had subplots in it with a lot of re-positioning and modifications. This figure followed the code (did not pop open and saved correctly, but when I tried to double click it in the windows folder, it did not open.

What fixed this has adding a

 set(figure2,'Visible','on')

right after the

set(figure2,'ResizeFcn','set(gcf,''visible'',''on'')');

followed by the save function and the close all windows.

Don't know why the ResizeFcn didn't turn this plots with subplots back to visible, but what I've got right now is working great!

Subject: save figures without displaying

From: Joe

Date: 2 Sep, 2014 17:14:11

Message: 19 of 19

> Jessie's
>
> figure2 = figure
> set(figure2, 'visible','off')
> set(figure2,'ResizeFcn','set(gcf,''visible'',''on'')');
> saveas(figure2,'name','fig');
>
> worked for me too.
>
> I had one thing that didn't work though. One of my figures had subplots in it with a lot of re-positioning and modifications. This figure followed the code (did not pop open and saved correctly, but when I tried to double click it in the windows folder, it did not open.
>
> What fixed this has adding a
>
> set(figure2,'Visible','on')
>
> right after the
>
> set(figure2,'ResizeFcn','set(gcf,''visible'',''on'')');
>
> followed by the save function and the close all windows.
>
> Don't know why the ResizeFcn didn't turn this plots with subplots back to visible, but what I've got right now is working great!

Now that I'm playing around with it, putting " set(figure2,'Visible','on') " does turn the figure back on. So still no solution for this plot with sub-plots.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us