Search code examples
matlabpopupfigureaxes

Avoid figure popping up continuously in MATLAB


How can I avoid MATLAB from popping up a GUI figure f with two axes while plotting data inside a loop. Here is a simple example:

f=figure;

ax.h1 = axes('Parent',f,'Position',[0.1 0.1 0.8 0.35],'Units','normalized');
ax.h2 = axes('Parent',f,'Position',[0.1 0.6 0.8 0.35],'Units','normalized');

for j=1:20
    axes(ax.h1)
    hold on
    plot(1:3,(1:3)+j)

    axes(ax.h2)
    hold on
    plot(1:3,(1:3)+1+j)

    pause(2)

end

I need to keep plotting data for several hours. So, it would be great if MATLAB didn´t pop up each time a new plot is generated.

Thanks!


Solution

  • As @TasosPapastylianou noted, the axis call is bringing the window to the front. Remove the axis and hold on calls within the loop and use plot(ax.h1, ... to plot to a specific axis. You only need to call hold on once for each axis, so do this at the start using hold(ax.h1, 'on') etc. Then your graphs should continue to update in the background without coming to the front each time.

    Your example becomes:

    f=figure;
    
    ax.h1 = axes('Parent',f,'Position',[0.1 0.1 0.8 0.35],'Units','normalized');
    ax.h2 = axes('Parent',f,'Position',[0.1 0.6 0.8 0.35],'Units','normalized');
    hold(ax.h1, 'on')
    hold(ax.h2, 'on')
    
    for j=1:20
        plot(ax.h1, 1:3,(1:3)+j)
        plot(ax.h2, 1:3,(1:3)+1+j)
    
        pause(2)
    
    end