Optimize live plotting with a large data set

조회 수: 11 (최근 30일)
Khang Nguyen
Khang Nguyen 2023년 9월 8일
댓글: Les Beckham 2023년 9월 8일
Hi,
I have a large data set (16 000 datas) that I am trying do a live plots. But I notice that it getting slower as the code progress, are there anyway to optimize it and make the code faster?
function [] = live_plot(table)
% this function will perfrom live plotting using an input array
graph_len = size(table);
figure
for i = 2:graph_len(1)
% graph number 1
subplot(2,1,1)
hold on
plot(table(i-1:i,1),table(i-1:i,2), "b")
plot(table(i-1:i,1),table(i-1:i,3), '--', 'Color',[1 0 0])
hold off
%graph number 2
subplot(2,1,2)
hold on
plot(table(i-1:i,1),table(i-1:i,4), "b")
plot(table(i-1:i,1),table(i-1:i,5), '--', 'Color',[1 0 0])
hold off
pause(0.00001)
end
end

답변 (1개)

Les Beckham
Les Beckham 2023년 9월 8일
편집: Les Beckham 2023년 9월 8일
You definitely don't want to call plot repeatedly inside your loop.
Here is a suggested approach where I created the plot outside the loop and then just update the data for each of the lines in the plot during the loop. Note that running this in Answers won't show the animation. It will just show the final result.
This definitely runs faster than your current code (I tried it on my desktop Matlab).
Adapt as needed.
You might also want to try the animated line function.
t = (0:1e-3:6*pi).';
data = [t sin(t) cos(t) 2*sin(t) -2*cos(t)]; % example data (18850 rows of data)
figure
% initially, just plot the first two points to initialize the plot
h = plot(data(1:2,1), data(1:2,2), data(1:2,1), data(1:2,3), data(1:2,1), data(1:2,4), data(1:2,1), data(1:2,5));
grid on
xlim([0 max(t)]) % manually set the x and y limits so Matlab won't automatically adjust while looping
ylim([1.5*min(min(data(:,2:5))) 1.5*max(max(data(:,2:5)))])
for i = 3:numel(t)
for iLine = 1:numel(h)
h(iLine).XData = [h(iLine).XData data(i,1)]; % add points one-by-one to the existing lines
h(iLine).YData = [h(iLine).YData data(i,iLine+1)];
end
drawnow limitrate
end
  댓글 수: 2
Khang Nguyen
Khang Nguyen 2023년 9월 8일
I have multiple plots like this I want to update at the same time! I am not sure how to include them using your method!
Les Beckham
Les Beckham 2023년 9월 8일
You just need to keep the handles to each of the subplots and then update as I did above.
Here's an example.
t = (0:1e-3:6*pi).';
data = [t sin(t) cos(t) 2*sin(t) -2*cos(t)];
figure
num_subplots = 4;
for iPlot = 1:num_subplots % create the subplots and plot the first two points to initialize the plot
hs = subplot(2,2,iPlot);
h(iPlot) = plot(data(1:2,1), data(1:2,iPlot+1));
set(hs, 'XLim', [0 max(t)])
set(hs, 'YLim', [1.5*min(min(data(:,iPlot+1))) 1.5*max(max(data(:,iPlot+1)))])
grid on
end
for i = 3:numel(t)
for iLine = 1:numel(h)
h(iLine).XData = [h(iLine).XData data(i,1)]; % add points one-by-one to the existing lines
h(iLine).YData = [h(iLine).YData data(i,iLine+1)];
end
drawnow limitrate
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by