【问题标题】:Matlab: add a dynamically updated progress bar in a GUI windowMatlab:在 GUI 窗口中添加动态更新的进度条
【发布时间】:2019-05-15 06:17:22
【问题描述】:

我正在尝试在 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 中添加动态进度条?

【问题讨论】:

    标签: matlab matlab-figure


    【解决方案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
    

    【讨论】:

    • 我有一个简短的后续问题。当我在 GUI 中用文本指示器替换进度条时,我发现了同样的问题。你有动态更新文本字符串的想法吗?还在画吗?
    • 我已经验证 'drawnow' 也适用于更新文本字符串!
    • 是的,drawnow 通常会更新数据。如果图形包含 text 对象,它也应该得到更新
    猜你喜欢
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多