MATLAB小记_有趣的waitbar的用法

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

标签: 

杂谈

 
分类: Matlab

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);

结果如上面的所示。效果一致……

success……哈哈……

原文地址:https://www.cnblogs.com/psztswcbyy/p/7455291.html