我正在尝试在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中添加动态进度条吗?
答案 0 :(得分:1)
在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