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.
Instead of doing a high-level
plotcall, consider adjusting the line handle properties, more specifically theXDataandYData, in the loop:Doing it this way, a
tic/tocbefore/after the code gives 0.09 seconds; a naiveplotas you have, as you have probably seen, gives a runtime of nearly 20 seconds.Note that I only used
getin this example to generate the dataset; I assume for a real time plot you’ve got someDatasetXandDatasetYto 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, justsetthe line’s entireXDataandYData.Finally, note that this
setcall 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
animinatedlineobject makes it easier to plot points from streaming data: