matlab gui uiwaitbar,matlab GUI中的waitbar

waitbar

作用:

这个函数的作用是打开或者更新进度条。

语法结构有:

(1)h = waitbar(x,'message')

(2)waitbar(x,'message','CreateCancelBtn','button_callback')

(3)waitbar(x,'message',property_name,property_value,...)

(4)waitbar(x)

(5)waitbar(x,h)

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

应用1:

语法结构h = waitbar(x,'message'); %其中x必须为0到1之间的数,message为显示的信息,其实这个x大于1也可以啊,但是进度条总在满的状态,x是多少就对应进度条显示的比例是多少

举例:hwait=waitbar(0,'请等待>>>>>>>>'); %这个0显示的是进度条的位置,因为是0,就在起始位置,还有就是这个hwait就是这个waitbar函数的句柄

得到:

cd3f2ae4d2a1

应用2

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函数,也就是说运行完成后让此进度条消失。

结果如下所示:

显示正在运行中:

cd3f2ae4d2a1

显示即将完成:

cd3f2ae4d2a1

应用3:

语法结构waitbar(x,'message','CreateCancelBtn','button_callback');%为的是在进度条上添加取消按钮,用户可以通过进度条来终止程序的运行。

举例:

编写函数如下;

function [valueofpi step] = approxpi(steps)

% Converge on pi in steps iterations, displaying waitbar.

% User can click Cancel or close button to exit the loop.

% Ten thousand steps yields error of about 0.001 percent.

h = waitbar(0,'1','Name','Approximating pi...',...

'CreateCancelBtn',...

'setappdata(gcbf,''canceling'',1)');

setappdata(h,'canceling',0)

% Approximate as pi^2/8 = 1 + 1/9 + 1/25 + 1/49 + ...

pisqover8 = 1;

denom = 3;

valueofpi = sqrt(8 * pisqover8);

for step = 1:steps

% Check for Cancel button press

if getappdata(h,'canceling')

break

end

% Report current estimate in the waitbar's message field

waitbar(step/steps,h,sprintf('.9f',valueofpi))

% Update the estimate

pisqover8 = pisqover8 + 1 / (denom * denom);

denom = denom + 2;

valueofpi = sqrt(8 * pisqover8);

end

delete(h)%删除进度条,不要去使用close函数关闭。

调用函数:

[estimated_pi steps] = approxpi(10000);

运行结果:

cd3f2ae4d2a1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值