More performance when updating graph

1 view (last 30 days)
Cedric Dunsch
Cedric Dunsch on 1 Apr 2022
Commented: Steven Lord on 1 Apr 2022
Hello Alltogether,
I'm programming the software for a testbench, there with like 20Hz measurementdata is produced.
The data (Time, Charge,Temperature, Voltage and Current) is saved in three arrays.
Goal is to show a plot with all measurementdata while running the tests.
I want to have a live plot with all measurement data, but while running the prgramm the Array is getting bigger (in the neighbourhood of 2 milion points) and the Plot function is to slow.
At the moment the plot is done by replotting every run of the loop (see Method Update Plot). Is there a way, just to add the new measured values to the existing plot, to gain some performance by not having to replot all the old values that are the same?
classdef plot_controller
%plot_controller: responsible to represent the measured live data.
properties
fig
end
methods
function [obj] = generate_plots(obj)
%generate_plots: generates and prepares figures
% needs: obj
obj.fig = figure;
subplot(2,2,1);
xlabel('time / s');
ylabel('charge / C');
title('Cell charge over time');
subplot(2,2,2);
xlabel('time / s');
ylabel('temperature / °C');
title('Cell temperature over time');
subplot(2,2,[3,4]);
xlabel('time / s');
title('Cell current and voltage over time');
yyaxis left
ylabel('voltage / V')
yyaxis right
ylabel('current / A')
end
function update_plots(obj,charge,voltage,current,temperature,time)
%update_plots: called every loop iteration to update plots
% needs: obj,charge,voltage,current,temperature,time
subplot(2,2,1);
plot(charge,time);
xlabel('time / s');
ylabel('charge / C');
title('Cell charge over time');
subplot(2,2,2);
plot(time,temperature);
xlabel('time / s');
ylabel('temperature / °C');
title('Cell temperature over time');
subplot(2,2,[3,4]);
xlabel('time / s');
yyaxis left
plot(time,voltage);
ylabel('voltage / V')
yyaxis right
plot(time,current);
ylabel('current / A')
legend('voltage','current')
end
end
end

Answers (1)

Geoff Hayes
Geoff Hayes on 1 Apr 2022
@Cedric Dunsch - every time you call plot a new graphics object is created for the axes. I recommend that you create one plot graphics object for each of your four desired plots, and update that whenever your function is called. In order to do that, you will need to save the handle to the plot graphics object and then set the data for that plot on subsequent calls. Something like the following
classdef plot_controller
%plot_controller: responsible to represent the measured live data.
properties
fig
hPlotChargeVsTime
end
methods
function [obj] = generate_plots(obj)
%generate_plots: generates and prepares figures
% needs: obj
obj.fig = figure;
subplot(2,2,1);
obj.hPlotChargeVsTime = plot(NaN,NaN);
xlabel('time / s');
ylabel('charge / C');
title('Cell charge over time');
end
function update_plots(obj,charge,voltage,current,temperature,time)
%update_plots: called every loop iteration to update plots
% needs: obj,charge,voltage,current,temperature,time
if (isnan(obj.hPlotChargeVsTime.XData))
set(obj.hPlotChargeVsTime, 'XData', time, 'YData', charge);
else
xData = [get(obj.hPlotChargeVsTime, 'XData') time];
yData = [get(obj.hPlotChargeVsTime, 'YData') charge];
set(obj.hPlotChargeVsTime, 'XData', xData, 'YData', yData);
end
end
end
end
I'm not sure if this will improve performance though especially as you say there are 2 million points in the array. In the above code, I'm assuming that only the latest points are passed to the update_plots method. If that is not the case, and arrays are passed instead, then you will need to adjust your code so that only the most recent data is passed. Note that I don't know if this will help (given the number of data points) but you will have only 4 plot objects rather than the many more.
  1 Comment
Steven Lord
Steven Lord on 1 Apr 2022
That's the first of the animation techniques listed in the "Animation Techniques" topic in the Animation section of the documentation.
2 million points probably fits in memory, but it might be worth seeing if storing the data as a tall array would be worthwhile, especially if you're planning to expand this analysis to even more data in the future. The plot function is supported for use with a tall array input.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!