I'm new to matlab and I want to plot some data in real time.
My approach was follows:
figure;
hold on;
for i = 1:1000;
plot(i, i);
drawnow;
end
But it has poor performance.
I also found a suggestion here on stackoverflow: https://stackoverflow.com/q/3118918/1066838
but only the last set data are drawn, so I see always only one point on the figure.
Best Answer
Instead of doing a high-level
plot
call, consider adjusting the line handle properties, more specifically theXData
andYData
, in the loop:Doing it this way, a
tic
/toc
before/after the code gives 0.09 seconds; a naiveplot
as you have, as you have probably seen, gives a runtime of nearly 20 seconds.Note that I only used
get
in this example to generate the dataset; I assume for a real time plot you've got someDatasetX
andDatasetY
to plot, so you'll need to work your data accordingly. But in the end, once you've got the dataset you want to plot at a particular time, justset
the line's entireXData
andYData
.Finally, note that this
set
call gets a bit unwieldy for very large datasets, since we have to set the the lines' data every time rather than append to it. (But it's certainly still faster than usingplot
.) This might be good enough depending on how frequently your dataset changes. See this question for more details.EDIT: As of MATLAB R2014b, the
animinatedline
object makes it easier to plot points from streaming data: