Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

save figures without displaying

749 views
Skip to first unread message

Oliver

unread,
Apr 14, 2011, 6:52:04 AM4/14/11
to
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?

Grzegorz Knor

unread,
Apr 14, 2011, 7:39:04 AM4/14/11
to
"Oliver" wrote in message <io6jkk$5em$1...@fred.mathworks.com>...

Try this code:

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

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

best regards
Grzegorz

Oliver

unread,
Apr 14, 2011, 8:24:05 AM4/14/11
to
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?

John

unread,
Apr 14, 2011, 8:57:20 AM4/14/11
to
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.

Grzegorz Knor

unread,
Apr 14, 2011, 9:01:21 AM4/14/11
to
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

Yuri Geshelin

unread,
Apr 14, 2011, 9:34:08 AM4/14/11
to
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>...

Oliver Woodford

unread,
Apr 19, 2011, 4:56:04 AM4/19/11
to
"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.

Yuri Geshelin

unread,
Apr 20, 2011, 9:07:05 PM4/20/11
to
"Oliver" wrote in message <io6jkk$5em$1...@fred.mathworks.com>...

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&#8217;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.

Lisa Thierbach

unread,
Oct 30, 2012, 12:39:08 AM10/30/12
to
"Yuri Geshelin" wrote in message <ionvvp$j8a$1...@fred.mathworks.com>...
> 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.

Tor Inge Birkenes

unread,
Dec 13, 2012, 7:21:08 AM12/13/12
to
> 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)

Steven_Lord

unread,
Dec 13, 2012, 9:43:18 AM12/13/12
to


"Tor Inge Birkenes" <tor.inge.birke...@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
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Yuri Geshelin

unread,
Dec 13, 2012, 5:29:08 PM12/13/12
to
"Tor Inge Birkenes" wrote in message <kach7k$84p$1...@newscl01ah.mathworks.com>...
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.

Yuri Geshelin

unread,
Dec 13, 2012, 5:35:11 PM12/13/12
to
"Steven_Lord" <sl...@mathworks.com> wrote in message <kacpi6$8g9$1...@newscl01ah.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?

Tor Inge Birkenes

unread,
Dec 14, 2012, 3:07:08 AM12/14/12
to
"Yuri Geshelin" wrote in message <kadl6v$qg7$1...@newscl01ah.mathworks.com>...
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

Jesse Hopkins

unread,
Nov 1, 2013, 6:23:08 PM11/1/13
to
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'')');

Bo Zhang

unread,
Nov 22, 2013, 11:40:22 AM11/22/13
to
"Oliver" wrote in message <io6jkk$5em$1...@fred.mathworks.com>...
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.

Benjamin

unread,
Feb 11, 2014, 1:14:10 PM2/11/14
to
"Oliver" wrote in message <io6jkk$5em$1...@fred.mathworks.com>...
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.

Joe

unread,
Aug 29, 2014, 2:10:09 PM8/29/14
to
"Benjamin Avants" wrote in message <lddp9i$r70$1...@newscl01ah.mathworks.com>...
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!

Joe

unread,
Sep 2, 2014, 1:14:11 PM9/2/14
to
> 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.

Erik

unread,
Jan 29, 2015, 9:23:13 AM1/29/15
to
Welcome to 2015 to this thread. In the latest MATLAB release (R2014b) openfig.m seems to have changed, because the code Yuri refers to is non-existent. I solved the problem from the original post in this way, read here:

http://stackoverflow.com/questions/28174583/how-to-edit-property-of-figure-saved-in-fig-file-without-displaying-it/28211771?iemail=1&noredirect=1#comment44796899_28211771

I asked the same question over there, but still the solution is not exactly what I want: edit the property in the .fig file instead of editing the property when the file is opened. This comes in handy when you open your .fig files on another computer running a different MATLAB installation.

Yuri Geshelin

unread,
Jan 29, 2015, 11:17:11 AM1/29/15
to
"Erik" wrote in message <madfoa$amh$1...@newscl01ah.mathworks.com>...
> Welcome to 2015 to this thread. In the latest MATLAB release (R2014b) openfig.m seems to have changed, because the code Yuri refers to is non-existent. I solved the problem from the original post in this way, read here:
>
> http://stackoverflow.com/questions/28174583/how-to-edit-property-of-figure-saved-in-fig-file-without-displaying-it/28211771?iemail=1&noredirect=1#comment44796899_28211771
>
> I asked the same question over there, but still the solution is not exactly what I want: edit the property in the .fig file instead of editing the property when the file is opened. This comes in handy when you open your .fig files on another computer running a different MATLAB installation.

Thanks Eric for your post. It has been 4 years since the discussion started.
I don't have R2014b, but regardless of the latest version of OPENFIG, you can always edit it in such a way that the 'VISIBLE' property of the figure will be set to 'ON'. Of course, it is preferable to keep the original matlab functions intact, which is why I suggested using the edited version of OPENFIG. I don't think that copying it to another directory is too much tampering with original MATLAB files. On the contrary, the tampering is what you suggested on the other forum -- change the original openfig.m. It is for this reason the folder has no write permission by default - to discourage the users from doing it.

Also, keeping both original and edited versions of OPENFIG gives you more flexibility. You can plug the edited version in and out by keeping it in a separate folder, by adding that folder to (or removing from) the path. This addresses the remark you made on the other forum:

"All opened .fig files containing figures are made visible now, also when opening directly from the explorer."

Depending on your needs, you can always choose one of the 2 scenarios: when you don't want a figure to become visible upon opening, switch off the edited version of OPENFIG.

I never ran across the loadFigure error, so can't comment on this. Your suggestion
"to edit the property in the .fig file instead of editing the property when the file is opened"
must be doable, you just need to know the format of FIG files.

Finally, you can resort to my other suggestion in this topic: set up listeners. They have to be switched on / off, too.

Erik

unread,
Jan 30, 2015, 6:46:19 AM1/30/15
to
This is a new solution based on the solution by Jesse Hopkins in post 15.

I tried the 'ResizeFcn' trick, but it fails on my installation: Windows8x64, MATLAB R2014b. Looking in the documentation on figure properties, I found a similar 'CreateFcn' property. This function does the trick, because 'CreateFcn' is called after the figure and its properties (including 'Visible' being 'off') have been loaded. So using:

set(gcf,'CreateFcn','set(gcf,''Visible'',''on'')')

on an invisible figure, then you save it as a .fig file, makes the figure become visible after you open the .fig file from your explorer.

I expect this works on figures with complex subplots etc. as well, since the main figure's 'Visible' property is the only thing that is set from 'off' to 'on'.

Yuri Geshelin

unread,
Jan 30, 2015, 9:07:12 AM1/30/15
to
"Erik" wrote in message <mafqu6$h60$1...@newscl01ah.mathworks.com>...
I tried this, it did not work for me. Here is what I tried:

close all
plot(1:10)
set(1,'vis','off')
set(1,'createfcn','set(gcf,''visible'',''on'')')
saveas(1,'tstvis.fig')
close all
open('tstvis.fig')

The figure flashes for a split second, then becomes invisible.

This holds true for:

MATLAB Version 8.1.0.604 (R2013a), Windows 7
MATLAB Version 7.5.0.342 (R2007b), Windows 7
MATLAB Version 7.7.0.471 (R2007b), SunOS 5.10

Marco

unread,
Jan 30, 2015, 9:20:24 AM1/30/15
to
"Erik" wrote in message <mafqu6$h60$1...@newscl01ah.mathworks.com>...
> This is a new solution based on the solution by Jesse Hopkins in post 15.
> Windows8x64 - MATLAB R2014b:
> using set(gcf,'CreateFcn','set(gcf,''Visible'',''on'')')
> on an invisible figure, then you save it as a .fig file, makes the figure become visible after you open the .fig file from your explorer.

Just found the thread, running today into excactly the same problem. It works! Isn't it luck for for me, that Erik published his post with updated information for R2014b users right this morning?! Thanks so much!

Yuri Geshelin

unread,
Jan 30, 2015, 9:50:13 AM1/30/15
to
"Marco" <MATLABn...@mobildata.de> wrote in message <mag3v1$9oi$1...@newscl01ah.mathworks.com>...
Why doesn't it work for me then? Is it because I am not running R2014b? I doubt it -- R2013a should be good enough.
Marco, could you please provide the precise set of commands that you used? This will help me figure where I am wrong.
Thanks

Yuri Geshelin

unread,
Jan 31, 2015, 8:53:14 AM1/31/15
to
"Marco" <MATLABn...@mobildata.de> wrote in message <mag3v1$9oi$1...@newscl01ah.mathworks.com>...
Confirmed: 'CreateFcn' doesn't work in MATLAB R2014b either. So unless Eric and Marco post the set of commands they used, I will assume that they are mistaken.

If, however, 'CreateFcn' is replaced with 'ResizeFcn' then the set of commands I posted in message #23 is working. But this is precisely what Jesse Hopkins had proposed on 1 Nov, 2013 (message 15), and it is a great solution. However, Joe reported a strange effect in the case when a figure has subplots and a lot of re-positioning and modifications (message 18). I double-checked: indeed, with complex .FIG files some subtle features are not reproduced upon opening the figure.

I then tried slightly different approach (also based on ResizeFcn, thanks Jesse!) which worked well for me:

close all
plot(1:10)
set(1,'vis','off','resizefcn','g_old = gcf;set(copyobj(g_old,0),''visible'',''on''),close(g_old)')
saveas(1,'tstvis.fig')
close all
open('tstvis.fig')

Also, instead of plot(1:10), I tried this on a complex figure, and it works as well.
This approach is not suitable for big (100 Mb+) files, as it takes up a lot of memory.

P.S. Don't even try 'CreateFcn' in this context - it will crash Matlab due to a set of recursive calls to HGLOAD.

Marco

unread,
Feb 2, 2015, 7:08:11 AM2/2/15
to
"Yuri Geshelin" wrote in message <maimo5$cb$1...@newscl01ah.mathworks.com>...
> Confirmed: 'CreateFcn' doesn't work in MATLAB R2014b either. So unless Eric and Marco post the set of commands they used, I will assume that they are mistaken.

I simply used the following code with in Win7Pro-64bit, R2014b with Image Processing Toolbox. All the best! Marco.

h_MyFigure = figure;
set(h_MyFigure,'Visible','off');

MY CODE HERE, i.e. SIMPLY: imshow(I);

set(h_MyFigure,'CreateFcn','set(gcf,''Visible'',''on'')');
saveas(h_MyFigure,myfilename,'fig');
close(h_MyFigure);

Yuri Geshelin

unread,
Feb 3, 2015, 9:31:11 AM2/3/15
to
"Marco" <MATLABn...@mobildata.de> wrote in message <manpb6$pqi$1...@newscl01ah.mathworks.com>...
Thanks Marco. Still not sure why it doesn't work for me, but it's not important, because ResizeFCn works.

Joe

unread,
Feb 24, 2015, 4:36:24 PM2/24/15
to
"Yuri Geshelin" wrote in message <maqm3a$bt9$1...@newscl01ah.mathworks.com>...
I tried the 'CreateFcn' approach and this worked for me, including the complicated subplots. I am using R2014b. Thanks everyone for your testing!

Andrew Fried

unread,
Mar 28, 2016, 10:45:09 AM3/28/16
to
Dear MATLAB,
Can this be logged as an official bug?

Intuitively, a user normally wants to open a figure in order to view it on their monitor when they double-click from windows explorer. I have used the invisible feature for a long time and saved figures without making them visible. However, I don't recall ever having to make them visible in the past in order to re-open them later. I agree with the comments of others that it does not make sense to change a built in function. I don't see any harm in setting the default for all of MATLAB users a value of openfig.m to visible. For those niche applications where someone wants to open a figure and not view it, they are undoubtably using lines of code anyway.

I believe this application is fairly common (since this thread already existed when I google searched it and others have contributed a great deal already). Within a large for loop, I am reading through a number of input files. I create a new figure (or multiple figures) for each file and set the visible property to off: set(h1,'Visible','off');

Then, I create various subplots within the figure, use the saveas feature to save as .FIG, and close the handle.

After reading this post, I learned I could use the following command to open the figure: openfig('myfigure.fig','visible'); However, it still does not work to open from windows explorer or from within the current folder of the matlab window (R2015b). I tried the makevisible function listed in this post, but it did not work for me. The only solution that works is the openfig command with 'visible' property. So, I suggest one of two solutions: change the saveas function to default .FIG files to visible even if they are currently not visible OR change the openfig function so it defaults all figure files to visible if no visibility property is specified.

Thanks,
Andrew

"Igor Varfolomeev" <ivarfo...@slb.com> wrote in message <n259mf$2um$1...@newscl01ah.mathworks.com>...
> And, just to sum up all known approaches to «save invisible figure as visible (without displaying it on your monitor even for a second), so that resulting “.fig” file would contain “visible” figure (you would be able to open it by double-clicking and it would be visible, without additional coding)» task:
>
> • Edit “fig” as mat file (problem: seem to not work in R2015b) [ http://www.mathworks.com/matlabcentral/answers/43340#comment_225135 ]
> • Plot figure outside of the visible area (problem: taskbar icon is still visible) [ http://www.mathworks.com/matlabcentral/answers/34400-saveas-visible-off#answer_43224 ]
> • Assume “ResizeFcn” is called on figure load (problem: should consider possible interference with some other ResizeFcn, does not work since R2014b) [ www.mathworks.com/matlabcentral/newsreader/view_thread/306249#930952 ]
> • Assume “CreateFcn” is called on figure load (problem: should consider possible interference with some other CreateFcn, works only since R2014b) [ http://stackoverflow.com/a/28234497/1032586 ]
> • Use modified copy of builtin hgsave() function (problem: need to modify hgsave() separately for each Matlab release) [ www.mathworks.com/matlabcentral/newsreader/view_thread/306249#940774 ]
>
> Other methods (not fulfilling all the mentioned requirements)
> • Simply make figure visible before saving and hide it after (problem: blinking)
> • Re-use single figure window, just to avoid bringing figure to the foreground [ http://www.mathworks.com/matlabcentral/newsreader/view_thread/306249#831909 ]
> • Write your own openfig() [ http://www.mathworks.com/matlabcentral/newsreader/view_thread/306249#832264 ]
> • hgload() with a property structure [ http://www.mathworks.com/matlabcentral/newsreader/view_thread/111338#281318 ]
> • openfig() with visibility [ http://www.mathworks.com/matlabcentral/newsreader/view_thread/108382#274180 ]
0 new messages