MATLAB: Figure Plot Slow

plotslow

A Matlab program plots DATA at run time. X axis is time, DATA is an event flag we monitor. If the event happens at time 2ms, then a dot is placed (plotted) at time 2ms. For each trial, we have a sequence of the event that happened at times. Y axis is Trial number. I used ‘hold on’ to plot on the same figure as new trials coming in. The problem is: at the beginning of the plot figure (new figure), the plot goes fast, but as more trials plotted, it becomes really slow. Could someone help me on what the cause could be? How to resolve? Thank you!

Best Answer

Instead of plotting more and more objects on the same plot, it is better to use scatter() and record the handle and then as more data comes in, set() the XData and YData properties of the handle to an array that includes all of the data.
h = scatter(...)
set(h,'XData', [get(h, 'XData'), newxvals], ...
'YData', [get(h, 'YData'), newyvals])