Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress figures?

Tags:

plot

matlab

How can I suppress the display of a figure window while keeping the plotting in the background for saving the resulting plot at the end of run? What is the best practice to do this? At present, my code is like this:

showPlot = 1; % switch to turn plotting on/off

fig = figure(1); clf; hold on;
lineHandle = line(nan, nan);
total = 0;

for i = 1:10000
    % long calculation
    total = total + 1;
    set(0, 'CurrentFigure', fig);
    xlim([0, total]);
    x = [get(lineHandle, 'XData'), total];
    y = [get(lineHandle, 'YData'), rand()];
    set(lineHandle, 'XData', x, 'YData', y);       
    drawnow;
end

% saveas(gcf, file, 'png');

I want to set up the code in such a way that when I set showPlot to 0, the figure window is not shown but the plot is saved to file.

like image 367
István Zachar Avatar asked Mar 07 '12 19:03

István Zachar


4 Answers

To make the current figure not visible:

set(gcf,'visible','off')
like image 173
Pursuit Avatar answered Oct 08 '22 21:10

Pursuit


From the MathWorks-reference:

To avoid showing figures in MATLAB you can start MATLAB using the noFigureWindows option. This option is not available on UNIX.

matlab -noFigureWindows

As an alternative you can change the default figure properties of the MATLAB root object:

set(0,'DefaultFigureVisible','off')

If you want to temporarily suppress new figures that should be accesable later in the same session you can save the figure handle:

set(0,'DefaultFigureVisible','off');

 %create invisible figure 1
 h(1)=figure;
 %create invisible figure 2 
 h(2)=figure;

 set(0,'DefaultFigureVisible','on');
 %show figure 1
 figure(1)

By the way, close all closes all currently open figures.

like image 29
Konstantin Schubert Avatar answered Oct 08 '22 22:10

Konstantin Schubert


The other answers were not working for me on R2015b on Ubuntu, my figure window would always show up.

I am processing 100+ files and the figure window popping up prevents me from using my computer while processing the files.

Here's a workaround, launch matlab without a display:

matlab -nodesktop -nodisplay

and this will prevent figure windows from showing up. Programmatically saving the figure to a file still works.

like image 43
Nick Avatar answered Oct 08 '22 21:10

Nick


As answered earlier, to suppress displaying figures during instantiation first call

set(0, 'DefaultFigureVisible', 'off');
% or, if post Matlab R2014b 
set(groot, 'DefaultFigureVisible', 'off');

After this call, creation of new figures in a script will not result in a visible window popping up. Naturally, the way to revert this setting is

set(0, 'DefaultFigureVisible', 'on');
% or, if post Matlab R2014b 
set(groot, 'DefaultFigureVisible', 'on');

The "gotcha" is that activating an existing figure for further manipulation will result in a visible window - if done incorrectly:

% suppress visible plot window creation
set(groot, 'DefaultFigureVisible', 'off');
figure(1); % will not result in a visible window
plot(0:.01:pi,sin(0:.01:pi));
hold on
figure(2); % still no visible window
plot(0:.01:10,(0:.01:10).^2);
% so far so good
% ... other statements ...
% select figure 1 to add to it:
figure(1); % visible window appears!
plot(0:.01:pi,cos(0:.01:pi));
hold off;
% ...

The workaround is to use another set command to select existing figures:

set(groot, 'DefaultFigureVisible', 'off');
figure(1); % will not result in a visible window
plot(0:.01:pi,sin(0:.01:pi));
hold on
figure(2); % still no visible window
plot(0:.01:10,(0:.01:10).^2);
set(groot, 'CurrentFigure', 1); % still no visible window
% plot commands will apply to figure 1
plot(0:.01:pi,cos(0:.01:pi));
hold off
% ...

Regardless of the setting of 'DefaultFigureVisible', calling

figure(h);

where h is a handle or integer for an existing plot window causes that window to become active and visible. Thus, one can make all plots visible at the bottom of a script this way:

fh = get(groot, 'Children');
for x = 1:numel(fh)
  figure(fh(x));
end
like image 1
NateT Avatar answered Oct 08 '22 22:10

NateT