12

I'm very new to MATLAB and I was trying to display a real time plot of some calculations. I have an N sized vector and I work with m values at a time (say m = N/4), so I want to plot the first m values and then as soon as the second m values are calculated have them replace the first plot.

My approach was as follows:

for i=1:N,
  ...
  //compute m
  ...
  plot(m);
end;

but it fails to update the plot in every loop and waits for all the loops to finish to plot the data. My question is: Should I use another function instead of plot or could I add some delay in each loop?

I think there must be a way I'm not aware of for updating the plot instead of re-plotting it every time.

gnovice
  • 125,304
  • 15
  • 256
  • 359
kirbuchi
  • 2,274
  • 2
  • 23
  • 35

2 Answers2

33

As Edric mentioned, you'll definitely want to include a drawnow command after the call to plot to force an update of the graphics. However, there is a much more efficient and smoother method to animate plots that doesn't involve recreating the entire plot each time. You can simply initialize your plot, capture a handle to the plot object, then modify the properties of that object in your loop using the set command. Here's an example:

hLine = plot(nan);         % Initialize a plot line (which isn't displayed yet
                           %   because the values are NaN)
for i = 1:N                % Loop N times
  ...
  % Compute m here
  ...
  set(hLine, 'YData', m);  % Update the y data of the line
  drawnow                  % Force the graphics to update immediately
end

In addition, before your loop and after the call to plot you can set a number of axes properties, like the axes limits, etc., if you want the axes to stay fixed and not change their appearance with each new vector m that is plotted.

gnovice
  • 125,304
  • 15
  • 256
  • 359
  • Thanks, that did the trick! Both yours and Edric's solution worked very well. But if I do it your way it won't have to replot everytime. – kirbuchi Jun 25 '10 at 17:46
  • @vvy I accepted the other one since it came first and helped me solve my problem, but I then realised this one was better. I guess the number of votes speaks for itself. – kirbuchi Feb 22 '14 at 18:34
  • 2
    @kirbuchi Usually people follow(only look at) the accepted answer. The *vote count* are not that *eye-catchy* as the **green-check-mark** ;) – vyi Feb 23 '14 at 13:34
  • @gnovice, could y please have a look at [real-time-plots-in-matlab-standalone-application](https://stackoverflow.com/questions/46696542/real-time-plots-in-matlab-standalone-application) – droid192 Oct 11 '17 at 20:00
11

You can add a call to DRAWNOW to force the plot to update. See the reference page. Note that DRAWNOW causes the graphics event queue to be flushed, which may cause callbacks etc. to be executed.

Edric
  • 23,676
  • 2
  • 38
  • 40
  • It has been some time now, but you might want to look at and use doublebuffering. I don't remember where to set that option - but it sure made a difference at that time :) – Chau Jun 25 '10 at 06:45
  • 2
    Double buffering is a property of the figure - "set( gcf, 'DoubleBuffer', 'on' )" or similar. Might help too, but you need a DRAWNOW to force the update. – Edric Jun 25 '10 at 07:45