views:

355

answers:

3

Hi, 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 (Say N=4m) values at a time so I want to plot the first m sized result 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 on 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.

+2  A: 

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
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
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
Indeed I was missing the drawnow command. Thanks a lot.
kirbuchi
+4  A: 

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
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
A: 
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

It seems that I can't get it to work I have to plot like 3 axis together. I've tried

hLine = plot(nan);       %# Initialize a plot line (which isn't displayed yet
                         %#   because the values are NaN)
for i = 1:length(normal_data{1}(:,3))-100              %# Loop N times
  if i == 1
      k = normal_data{1}(1+((i-1)*50):50*i,1);
      l = normal_data{1}(1+((i-1)*50):50*i,2);
      m = normal_data{1}(1+((i-1)*50):50*i,3);
  else
      k = [k; normal_data{1}(1+((i-1)*50):50*i,1)];
      l = [l; normal_data{1}(1+((i-1)*50):50*i,2)];
      m = [m; normal_data{1}(1+((i-1)*50):50*i,3)];
  end
      set(hLine,'XData',k);  %# Update the y data of the line
      set(hLine,'YData',l);  %# Update the y data of the line
      set(hLine,'ZData',m);  %# Update the y data of the line
      drawnow                %# Force the graphics to update immediately
end

What have I done wrong? Thank you

kznatt
For the record, you shouldn't post a question like this as an answer to another question. You should instead post it as a new question. You should delete this post, but I'll give you an answer first. If you want to plot points in 3-D space, you should use the function [PLOT3](http://www.mathworks.com/access/helpdesk/help/techdoc/ref/plot3.html) instead of the function [PLOT](http://www.mathworks.com/access/helpdesk/help/techdoc/ref/plot.html).
gnovice

related questions