0
votes

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!

1
Make the figure invisible... However, a much better approach would be to store the data, then plot everything in one go after it's ready. - Dev-iL
I may be wrong, but I believe the reason you get them "popping up" is because you call the "axes" command, which has as a side effect that that particular axes will be displayed and brought to the forefront. If you just want to "plot" to that axes, you can do so directly from the plot command, i.e. plot(ax.h1, bla bla bla). - Tasos Papastylianou
@Dev-iL. This is a GUI figure, with some buttons. So I want to keep looking to data. If I make the figure invisible my GUI will be invisible, and that cannot happen. If I keep storing data without ploting I cannot track what is going on. - hello123

1 Answers

2
votes

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