美文网首页
matlab GUI中的waitbar

matlab GUI中的waitbar

作者: b7701413df2b | 来源:发表于2017-05-23 22:47 被阅读0次

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函数的句柄

得到:

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

结果如下所示:

显示正在运行中:

显示即将完成:

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

运行结果:

相关文章

网友评论

      本文标题:matlab GUI中的waitbar

      本文链接:https://www.haomeiwen.com/subject/jmzgxxtx.html