Streamline function keeps overwriting plot in a for loop
Show older comments
I am trying to create a streamline plot of a 2D field that is evolving in time. I would like to visualize how the streamlines change over time and I am currently using a for loop to loop through each iteration in time. However, when I use the streamline function to plot it, I can't seem to clear the previous iteration of streamlines and instead they keep layering on top of the previous one. Using the close command on the figure does work, but this causes the figure to flash on the screen for a quick second before it closes again, and I am trying to get it to stay on the screen and keep updating like a normal plot would.
Answers (1)
Pratyush Swain
on 11 Dec 2023
Edited: Pratyush Swain
on 11 Dec 2023
Hi Shaan,
I understand you are unable to clear the the previous iteration of streamlines as it keeps layering on top of the previous ones. One workaround is to use the "cla" command to clear the axes which will delete all visible figures in each iteration. Please refer to the example implementation below:
% Load Dataset %
load wind
% Meshgrid for starting points %
[startX,startY] = meshgrid(80,20:10:50);
% Cell array to hold the streamline object handles %
lineobjs = cell(1,10);
% Looping over new set of data points in each iteration %
for i=1:10
xi = x(:,:,i);
yi = y(:,:,i);
ui = u(:,:,i);
vi = v(:,:,i);
% Compute the 2-D streamline vertex data %
verts = stream2(xi,yi,ui,vi,startX,startY);
% Visualize the 2-D matrix of vector fields by calling streamline function %
lineobjs{i} = streamline(verts);
% Add title and labels %
title(['Streamlines at time step ' num2str(i)]);
xlabel('X');
ylabel('Y');
% Add pause and then clear the figure object %
pause(0.5)
cla
end
The "cla" command along with "pause" function will help to visualize the streamlines over each iteration without any overlapping.For more information please refer to
Hope this helps.
Categories
Find more on Heat Transfer 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!