Is there a way to manually control and delay drawing with 'nexttile', similar to how 'subplot' behaves, to improve plotting performance?

2 views (last 30 days)
Problem:When using subplot in a loop, MATLAB only updates the figure at the end, unless drawnow is called each iteration, making it faster. With nexttile, the figure updates immediately for each tile, slowing down the process. Using drawnow limitrate helps but isn't ideal.
Question:Is there a way to manually control and delay drawing with nexttile, similar to how subplot behaves, to improve plotting performance?

Answers (1)

Ruchika Parag
Ruchika Parag on 26 Jun 2024
Hi Patrick,
Here is a brief example of how you can manually control and delay the drawing with nexttile to improve performance:
% Sample data
x = 1:100;
y1 = sin(x);
y2 = cos(x);
y3 = tan(x);
% Create a figure and set up the tiles
fig = figure;
tiledlayout(3,1)
% Create the tiles and store the axes handles
ax1 = nexttile;
ax2 = nexttile;
ax3 = nexttile;
% Plot the data in the tiles, but don't draw them yet
line(ax1, x, y1)
line(ax2, x, y2)
line(ax3, x, y3)
% Manually draw the tiles
drawnow
% You can now update the data in the tiles and call drawnow to redraw them
for i = 1:10
y1 = sin(x + i);
y2 = cos(x + i);
y3 = tan(x + i);
set(ax1.Children, 'YData', y1)
set(ax2.Children, 'YData', y2)
set(ax3.Children, 'YData', y3)
drawnow
pause(0.1) % Leverage the pause function with a small value to yield control back to MATLAB's event queue, which can help in managing the rendering updates
end
You can also use 'animatedline'instead of 'line':
% Create the tiles and store the animatedline objects
al1 = animatedline(ax1, 'Color', 'b');
al2 = animatedline(ax2, 'Color', 'r');
al3 = animatedline(ax3, 'Color', 'g');
for i = 1:10
y1 = sin(x + i);
y2 = cos(x + i);
y3 = tan(x + i);
addpoints(al1, x, y1)
addpoints(al2, x, y2)
addpoints(al3, x, y3)
drawnow
pause(0.1)
end

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!