Not sure whether it is a full solution, but one improvement in your code would definately be to move the figure()
command out of the loop.
Maybe this is already sufficient, or maybe you need to assign your image command to a handle as described in this related question , allowing you to remove the last plotted image before you plot a new one.