我正在使用 MATLAB R2024a,在实时编辑器中进行编辑,并在 while 循环中更新绘图,如何自动动画更新进度?
这是我的示例代码,更新 while 循环中的绘图。 当n设置>=9时,输出窗口自动生成动画和控制栏,可以预览进度并导出动画,但是,当n为< 8, The output result is just a figure. Is there anyway to generate the animation all the time, even if n is small?
% creating the date
clear;
x=randi(100,10);
y=randi(100,10);
scatter(x,y,"black");
%set loop times
n=9
while n>0
plotnodes(x(n),y(n));
%wait for user's input
input("Press any key to continue");
n=n-1;
end
% plot function
function plotnodes(x,y)
hold on;
plot(x,y,'ro',"MarkerSize",10,"LineWidth",2)
hold off;
end
您可以尝试添加drawnow来强制更新图形。
还要考虑到,当仅使用提示作为参数来调用 input 时,它期望用户输入要计算的表达式或数字。
如果您对用户提供的具体输入不感兴趣,可以添加
"s"
作为第二个输入参数;这将阻止 input
将用户输入评估为表达式。
作为
input
的替代方案,您可以使用 pause 暂时停止 MATLAB 执行
这是更新后的代码
% creating the date
clear;
x=randi(100,10);
y=randi(100,10);
scatter(x,y,"black");
%set loop times
n=9;
while n>0
plotnodes(x(n),y(n));
%
% Add drawnow to upated the plot
%
drawnow
%wait for user's input
%
% Add "s" to treat the input as test without evaluatng the expression
%
input("Press any key to continue","s");
n=n-1;
end
% plot function
function plotnodes(x,y)
hold on;
plot(x,y,'ro',"MarkerSize",10,"LineWidth",2)
hold off;
end