【问题标题】:Two Waitbars in MATLABMATLAB 中的两个等待栏
【发布时间】:2014-01-06 09:37:58
【问题描述】:

matlab 中是否可以有 2 个(多个)等待栏(进度条)? 就像一个窗口中有两个栏。

我知道我们可以使用用户定义的 GUI 来实现它并手动完成所有操作,但这将是一个漫长的过程。

【问题讨论】:

标签: matlab user-interface


【解决方案1】:

我推荐 Ben Tordoff 的 multiwaitbar,可在 MATLAB Central File Exchange 上找到。

【讨论】:

    【解决方案2】:

    如果不是必须在同一个图形窗口中,则给等待栏句柄并通过句柄调用它们;例如

    %% Script creating 2 wait bars in different windows
    
    bar1=waitbar(0,'bar1');         % creates 2 waitbars
    bar2=waitbar(0,'bar2');
    
    % updates bar2
    
    waitbar(completed_value_bar2,bar2,'updated message') % updated message is optional
    
    % updates bar1
    
    waitbar(completed_value_bar1,bar1,'updated message') % updated message is optional
    
    %
    delete(bar1)
    delete(bar2)
    

    如果有必要,可以使用以下方法,但会大大增加运行时间

    %% Script creating 2 wait bars in the same figure window
    
    
    bar1=waitbar(0,'this is bar1','CreateCancelBtn','foo1');   
    bar2=waitbar(0,'this is bar2','CreateCancelBtn','foo2');
    % foo1 represents function executed by cancel button 1 (similar for foo2)
    
    Pos=get(bar1,'OuterPosition');       
    info_bar1=findobj(bar1);        % gets waitbar object handles
    info_bar2=findobj(bar2);        % 
    set(bar1,'visible','off')       % hides the bars
    set(bar2,'visible','off')       %
    % generates intital figure window;
    F=figure;
    set(F,'position',[Pos(1:2),1.35*Pos(3),2*Pos(4)]); % resises figure (could be more elegant)
    loc1=get(info_bar1(2),'position');                 % get position for bar1 
    loc2=loc1+[0 50 0 0];                              % shifts bar2 up
    
    P = copyobj(info_bar1(2),F);    % Copy bar1 to new fig 
    
    % note the figure handle bar1(2) contains the waitbar & message for bar1
    
    set(P,'position',loc1)          % Sets position of bar 1
    Q = copyobj(info_bar2(2),F);    % Copy bar2 to new fig
    set(Q,'position',loc2)          % Sets position of bar 1
    
    
    button_loc1=get(info_bar1(3),'position');   % gets button location               
    button_loc2=button_loc1+[0 50 0 0];         % shifts button 2
    B1 = copyobj(info_bar1(3),F);               % adds buttons to figure 
    set(B1,'position',button_loc1)              % sets button location
    B2 = copyobj(info_bar2(3),F);
    set(B2,'position',button_loc2)
    
    %
    for a=1:100
    
        for b=1:100
    
            %some calculation
    
            % updates bar2
            completed_value_bar2=b/100;
            waitbar(completed_value_bar2,bar2,'updated message')
            delete(Q)
            Q = copyobj(info_bar2(2),F);
            set(Q,'position',loc2)
        end
    
        % updates bar1
        completed_value_bar1=a/100;
        waitbar(completed_value_bar1,bar1,'updated message')
        delete(P)
        P = copyobj(info_bar1(2),F);
        set(P,'position',loc1)
    
    end
    
    %
    delete(bar1)
    delete(bar2) 
    

    【讨论】:

    • 您的代码显示了很多经验,但最后两行 delete(bar1)delete(bar2) 不起作用。它还缺少一个重要功能取消按钮
    • 我还没有研究将取消按钮转移到新图像。删除命令应该删除隐藏的等待条,但它不是严格要求的,如果它们被替换(或前面)为 set(bar1,'visable','on'),并且对于 bar2 类似,则可以观察到效果。
    • 取消按钮(包括它执行的代码)是 info_bar1 和 info_bar2 的第三个条目,上面的代码被编辑为包括从每个条形图添加取消按钮
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多