How to speed up live plotting over time?
Show older comments
I have a loop running to record certain values over a time period of ~1hr and would like to watch them live. I've already got it set up but as the loop goes on the speed slows down.
I'm plotting a 60 second window worth of data at a time and having the incoming data slide along to the left.
function live_plot(voltages, plots, handles)
if voltages(3) > 60 % If there's more than 60sec worth of data
plots.v1.XData(1) = []; plots.v1.YData(1) = []; % Remove the first entry from each vector
plots.v7.XData(1) = []; plots.v7.YData(1) = [];
plots.v8.XData(1) = []; plots.v8.YData(1) = [];
plots.v9.XData(1) = []; plots.v9.YData(1) = [];
end
plots.v1.XData(end + 1) = voltages(3); % Add the time index to the end of each vector
plots.v7.XData(end + 1) = voltages(3);
plots.v8.XData(end + 1) = voltages(3);
plots.v9.XData(end + 1) = voltages(3);
plots.v1.YData(end + 1) = voltages(1); % Add each plot's respective voltage to the end of it's vecto
plots.v7.YData(end + 1) = voltages(2);
plots.v8.YData(end + 1) = voltages(4);
plots.v9.YData(end + 1) = voltages(5);
xlim(handles.plot1, [plots.v1.XData(1), max(plots.v1.XData(end), 60) + 5]); % Set the xlimits on each plot
xlim(handles.plot4, [plots.v7.XData(1), max(plots.v7.XData(end), 60) + 5]);
xlim(handles.plot5, [plots.v8.XData(1), max(plots.v8.XData(end), 60) + 5]);
xlim(handles.plot6, [plots.v9.XData(1), max(plots.v9.XData(end), 60) + 5]);
This is how I'm doing it. Voltages is the values I'm receiving to plot, handles is the axes, and plots is the plots. If the elapsed time is more than 60 seconds, then I get rid of the first value and add the new one to the end, so the window is always the same size. However, after about 10-15 minutes it starts to slow down substantially so that what I'm looking at is about 30 seconds behind what's happening live.
I checked with tic/toc to see if it was the reading of data or the plotting that was slowing down over time and it's definitely the plotting. You can see it gradually increasing below. At some point it slows down enough that I can't read data as fast as it's being sent anymore and the time delay starts increasing a lot.

Anyone have any ideas how to keep this from happening?
Answers (1)
Jan
on 27 Mar 2017
Cropping an element and appending another one recreates the array two times per iteration. This can exhaust the memory. Try this:
% Preallocate the arrays initially:
plots.v1.XData = nan(1, 60 * Freq);
handles.plotCursor = 0;
% Update the values:
if voltages(3) > 60
handles.plotCursor = handles.plotCursor + 1; % Return handles to the caller!
else
plots.v1.XData(1:handles.plotCursor - 1) = plots.v1.XData(2:handles.plotCursor);
end
plots.v1.XData(handles.plotCursor) = voltages(3);
And equivalently for the other vectors.
2 Comments
Jesse Bucksot
on 27 Mar 2017
Jan
on 27 Mar 2017
The created vectors are stored in the X/YData properties of the line objects. This means, that they are not deleted. Well, deleted from the workspace, but not from the memory.
Could you post a function to reproduce the problem based on random data? Currently I'm not sure which command causes the slow down.
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!