Matlab中的waitbar(进度条)的应用……(显示程序运行的百分比)

来源:互联网 发布:阿里云降低配置 编辑:程序博客网 时间:2024/05/14 22:41

来源:点击打开链接


Matlab中的help“waitbar”得到应用之一:

h = waitbar(x,'message');其中x必须为0到1之间的数,message为显示的信息,

举例如下:

hwait=waitbar(0,'请等待>>>>>>>>');

得到:

Matlab中的waitbar(进度条)的应用鈥︹Γㄏ允境绦蛟诵械陌俜直龋
应用之二:

waitbar(x,h,'updated message');

x为显示的进度,必须在0到1之间h为所建立的waitbar的句柄,updated message为实时显示的信息,此语句经常地用于for循环中,如下所示:

steps=100;
hwait=waitbar(0,'请等待>>>>>>>>');
for k=1:steps
    if steps-k<=5
        waitbar(k/steps,hwait,'即将完成');
        pause(0.05);
    else
        str=['正在运行中',num2str(k),'%'];
        waitbar(k/steps,hwait,str);
        pause(0.05);
    end
end
close(hwait); % 注意必须添加close函数

结果如下所示:

显示正在运行中:

Matlab中的waitbar(进度条)的应用鈥︹Γㄏ允境绦蛟诵械陌俜直龋

显示即将完成:

Matlab中的waitbar(进度条)的应用鈥︹Γㄏ允境绦蛟诵械陌俜直龋
上例子中,当循环步骤刚好是100,但是如果循环不是100的时候,要作小小的改变,举例如下:

steps=150;
hwait=waitbar(0,'请等待>>>>>>>>');
step=steps/100;
for k=1:steps
    if steps-k<=5
        waitbar(k/steps,hwait,'即将完成');
        pause(0.05);
    else
        PerStr=fix(k/step);
        str=['正在运行中',num2str(PerStr),'%'];
        waitbar(k/steps,hwait,str);
        pause(0.05);
    end
end
close(hwait);

delete(hwait); 


原创粉丝点击