Matlab:在 GUI 中添加一个动态更新的进度条 window

Matlab: add a dynamically updated progress bar in a GUI window

我正在尝试在 GUI 中添加动态进度条。我注意到有一些可用的解决方案 (How to add progress bar control to Matlab gui?)。我的方法基于创建两个不同颜色的面板,一个用于背景,另一个用于前景(即进度条)。我的代码如下:

bar = uipanel('Parent',handles.bgProgressBar,'BackgroundColor','r');
%Note: bgPogressBar is the tag of a panel manually added with GUIDE
barPosition = get(bar,'Position');
cnt = 0
for ii = 1:S
   for jj = 1:T

       do something 
       ….

       cnt = cnt + 1;
       progress = cnt/(S*T);
       barPosition(3) = progress;
       barPosition;
       set(bar,'Position',barPosition);   
   end
end

这里的问题是柱子不是实时更新的。它不会响应,但只会在循环完成时进行到最后。是否可以在 GUI 中添加动态进度条?

set 之后使用 drawnow 以便立即更新屏幕上的图形对象:

bar = uipanel('Parent',handles.bgProgressBar,'BackgroundColor','r');
%Note: bgPogressBar is the tag of a panel manually added with GUIDE
barPosition = get(bar,'Position');
cnt = 0
for ii = 1:S
   for jj = 1:T

       do something 
       ….

       cnt = cnt + 1;
       progress = cnt/(S*T);
       barPosition(3) = progress;
       barPosition;
       set(bar,'Position',barPosition);   
       drawnow %%%%%
   end
end