4

In Matlab, I have a loop that performs operations on arrays. I would like to display the array at each iteration (using "imagesc" for instance), but without needing user intervention.

I can force Matlab to update the displayed figure by inserting the command "pause" after imagesc, but it needs to be dismissed by a key press. Without the "pause" command, the figure is not updated before the end of the loop.

Is there a way to update the figure at each iteration of the loop ?

Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
calvin tiger
  • 391
  • 2
  • 7
  • 18
  • Duplicate: [MATLAB plot inside a loop](http://stackoverflow.com/q/2800066/52738), [Real time plot in matlab](http://stackoverflow.com/q/3115833/52738), and probably a few others... – gnovice Jan 26 '12 at 19:31

2 Answers2

4

Try using Matlab command drawnow after the graphing code within the loop.

drawnow causes figure windows and their children to update, and flushes the system event queue. Any callbacks generated by incoming events (e.g., mouse or key events) are dispatched before drawnow returns.

mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194
2

If drawnow updates too quickly, you can control the "frame rate" slightly better with pause(time_in_seconds). E.g., to pause for 0.5 seconds use

for ...
   % plot stuff
   pause(0.5);
end
gtownescapee
  • 116
  • 2