如何在Matlab中使用等待栏? [英] How can I get a waitbar to work in Matlab?

查看:206
本文介绍了如何在Matlab中使用等待栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有一个等待时间较长的操作等待栏.这是我的代码:

I want to have a waitbar for an operation that takes quite a while. Here is my code:

h = waitbar(0,'Please wait...');

for i=1:counterend
    waitbar(i/waitbarcounter)
    Atemp    = At+i*step;
    handle   = @(M) 1/M^2*((2/(gamma+1))*(1+(gamma-1)*M^2/2))^((gamma+1)/(gamma-1))-(Atemp/At)^2;
    Mach     = fzero(handle, 5);
    Aplot(i) =  Atemp/At;
    Tplot(i) = Tc / (1+(gamma-1)*Mach^2/2);
    Mplot(i) = Mach;
    plot(Aplot, Tplot)
end

close(h) 

Matlab给出的错误是:

The error Matlab gives is:

???在249处使用==>等待条时出错
等待栏的参数不正确

??? Error using ==> waitbar at 249
Improper arguments for waitbar

经过调查,我确信由于循环中存在环绕代码,因此必定会发生此错误.

After investigation, I am sure that this error must occur because of the sorrounding code in the loop.

注意:无需等待栏,循环就可以正常工作.

推荐答案

正在运行

counterend = 10000;
>> h = waitbar(0,'Please wait...');

for i=1:counterend
    waitbar(i/counterend)
end

close(h);

在2007a/Windows XP上可以正常工作.

Works as expected on 2007a / Windows XP.

在旁注中,这将有助于知道被抗衡的定义是什么.快速检查的方法是确保您没有通过CELL.

On a side note, it would help knowing what countered is defined as. Something quick to check would be to ensure that you are not passing it a CELL.

运行

counterend = {10000};
h = waitbar(0,'Please wait...');

for i=1:counterend
    waitbar(i/counterend)
end

close(h);

在2007a中产生了一个不同的错误(请参见下文),但此错误消息在2008年可能已更改.

Yields a different error (see below) in 2007a, but this error message may have changed in 2008.

???未定义的函数或方法 '_colonobj'用于输入参数 输入单元格".

??? Undefined function or method '_colonobj' for input arguments of type 'cell'.

最后一点建议是在大型阵列/数据集上使用等待条时要特别小心.尽管我认为将进度告知用户很重要,但对我来说,还需要考虑将多少时间添加到循环中.通过处理具有100k +项的数组,我成为了Profiler的虔诚用户,了解了时间的真正消耗.我可以告诉您,时间不在i/X的计算中,而是全部在更新等待栏的显示中.为了减轻更新/绘制的麻烦,我仅每100到1000个条目中更新了等待栏,就极大地帮助了我们.

My last bit of advice would be caution you on the use of waitbar for large arrays/data sets. While I think it is important to inform the user of the progress, to me there is also a concern for how much time is added to the loop. Working with arrays that have 100k+ entries, I became a religious user of the Profiler to see where the time was really being spent. I can tell you the time is not in the calculation of the i/X, it was all in updating the waitbar's display. To soften the blow of the update/drawnow, I only updated the waitbar every 100 to 1000 entry which helped tremendously.

更新了响应以匹配最新代码

Updated response to match latest code

我最初是在匿名部门开始攻击此问题的,过去曾遇到过麻烦,这是我的个人仇恨.当查看该函数时,我发现您正在使用gamma,您是否将此定义为常量(与循环/函数相同)?我问的原因是'gamma'是Matlab函数,在尝试自己运行函数时给了我错误.下面我稍微修改了您的代码,在这里确实可以正常运行.如果我做出的任何假设是错误的,请告诉我. 另外,如果您确实打算使用gamma函数,则该函数将缺少该函数的任何参数.希望这会有所帮助!

I first started to attack this problem at the anonymous function, having problems with them in the past it's a personal vendetta of mine. When looking into the function I found that you are using gamma, do you have this defined as a constant (constant to the loop / function)? The reason I ask is that 'gamma' is a Matlab function and was giving me errors when trying to run your function by itself. Below I have modified you code slightly and this does run fine here. If any of the assumptions I have made are wrong please let me know. In addition, if you did intend to use the gamma function, your function is missing any arguments to it. Hope this helps!

clc
h = waitbar(0,'Please wait...');
counterend = 1000;
waitbarcounter = counterend;
g_amma = 7;
At = 34;
step = 2;
Tc = 42;

for i=1:counterend
    waitbar(i/waitbarcounter)
    Atemp    = At+i*step;
    handle   = @(M) 1/M^2*((2/(g_amma+1))*(1+(g_amma-1)*M^2/2))^((g_amma+1)/(g_amma-1))-(Atemp/At)^2;
    Mach     = fzero(handle, 5);
    Aplot(i) =  Atemp/At;
    Tplot(i) = Tc / (1+(g_amma-1)*Mach^2/2);
    Mplot(i) = Mach;
    plot(Aplot, Tplot)
end

close(h) 

此致

亚当

这篇关于如何在Matlab中使用等待栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆