user3112939
user3112939

Reputation: 21

Displaying Constantly Updating Plots Simultaneously in Different Windows

I'm trying to create a program consisting of a main GUI that can call several separate GUIs to display distinct data that is updated continually in "real-time". However, it seems that only plots within a single GUI can be updated at the same time. Multiple axes in the same GUI can be simultaneously updated. If two GUIs are opened, however, only axes in a single GUI can be updated at one time, with axes in the other GUI "holding" until the other GUI stops trying to continuously plot.

As a simple test of this issue, I've created a GUI consisting of a single axes tagged 'plotAxes' and a togglebutton. When the togglebutton is "depressed", random data is continuously plotted in the axes using the code:

xData = linspace(1,20,20);
while(get(hObject,'Value'))
    yData = rand(20,1);
    plot(handles.plotAxes,xData,yData);
    drawnow;
end

With the 'gui_Singleton' parameter set to 0, I opened two instances of this GUI. If I toggle GUI A to start plotting, continuous random data is displayed in the axes. If I then toggle GUI B to plot, the display in GUI A holds at the last frame and GUI B starts to display continuous random data. When I un-toggle the GUI B plot button, GUI A resumes continuous plotting.

Is there any way to allow axes in separate GUI windows to update simultaneously? I tried using the 'batch' command to run the GUIs on separate workers, but the GUI does not display when started through 'batch' (I suspect 'batch' is not intended for anything graphics-related).

Upvotes: 2

Views: 2083

Answers (2)

Peter
Peter

Reputation: 14947

Run the plot functions from a timer object. This will let you keep the logic for the two axes separate: one timer per axis. See help timer, and set the TimerFcn property to the function you want to run when the timer expires.

for ii=1:2
  figure; ax(ii) = gca;
  tim(ii) = timer;
  tim(ii).ExecutionMode = 'fixedRate';
  tim(ii).Period = 0.5;

  % A timer callback function needs at least two parameters.
  % x,y are dummy parameters to make the function call happy,
  % but we will ignore the values
  tim(ii).TimerFcn = @(x,y)(plot(ax(ii), rand(10,3)));

  start(tim(ii));
end

EDIT: I forgot to mention that the timer object can be stored in the GUI data structure, and the toggle button callback need only start and stop the timer.

Upvotes: 2

Jonas
Jonas

Reputation: 74940

You need to control the axes from a single thread:

figure;ax(1) = gca;
figure;ax(2) = gca;

while all(ishandle(ax))
    plot(ax(1),rand(10,3));
    plot(ax(2),rand(10,3));
    drawnow
end

This will plot in both axes until you close one of the figures.

Upvotes: 1

Related Questions