如何监控长计算?

发布于 2025-01-07 10:40:56 字数 220 浏览 0 评论 0原文

我想设置一个计数器来通知我有关长时间迭代计算的信息(例如在 for 中)。

是否可以设置此计数器,以便当它在屏幕上更新时,它会替换以前的值?

也就是说,打印 for 的迭代器变量是不行,因为 Matlab 要么将其打印到新行中,要么打印在前一个值之后,但经过 10000 次迭代后,屏幕将被填充。另外,我想每轮更新计数器。

I would like to set up a counter that informs me about a long iterative computation (e.g. in for).

Is it possible to set up this counter in a way that when it is updated on screen, it replaces the previous value?

That is, printing the iterator variable of a for is not ok, since Matlab either prints it into a new line, or after the previous value, but after 10000 iterations the screen would be filled either way. Also, I would like to update the counter in each turn.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

那一片橙海, 2025-01-14 10:40:56
fprintf('\n')
for i=1:15
    fprintf([repmat('\b', 1, length(num2str(i-1))) '%d'], i)
    pause(0.1)
end
fprintf('\n')
fprintf('\n')
for i=1:15
    fprintf([repmat('\b', 1, length(num2str(i-1))) '%d'], i)
    pause(0.1)
end
fprintf('\n')
锦爱 2025-01-14 10:40:56

您可以使用 \b 打印退格字符。例如:

for i=1:10
    fprintf(1, '\b%d', i);
end

You can use \b to print a backspace character. e.g.:

for i=1:10
    fprintf(1, '\b%d', i);
end
裸钻 2025-01-14 10:40:56

我不久前做了这个函数,它画了一个漂亮的ascii进度条。基本上与你的问题的其他两个答案的想法相同,但封装得更多

function progressbar(percent, N, init, extrastr)
% Draws a progress bar in the matlab command prompt. Useful for lengthly
% calculations using for loops
%
% Arguments:
%   - percent:  A number between 0 and 1
%   - N:        how many characters wide the bar should be
%   - init:     (optional; default false) true or false; whether or not
%               this is the first time calling the progressbar function for
%               your current bar.
%   - extrastr: (optional; default char(10)) An extra string to append to
%               the progress bar. Things will go screwy at the command
%               console if this string changes length from call to call of
%               progressbar.
%
% Outputs:
%
% Usage Example:
%
%   for k=1:1000
%       progressbar(k/1000,50,k==1,sprintf('\n We are are on number%4d\n', k));
%       % fake a computation
%       pause(0.05);
%   end
%

    if nargin < 3
        init = 0;
    end
    if nargin < 4
        extrastr = char(10);
    end

    percent = min(max(real(percent),0),1);

    done = round(N*percent);
    done_str = '*'*ones(1, done);
    left_str = '-'*ones(1, N-done);
    bar = sprintf(['||' done_str left_str '|| %3d'],round(percent*100));

    erase = [];
    if ~init
        % use backspace characters to erase the previously drawn bar
        erase = ['' char(8)*ones(1,length(bar)+length(extrastr)+1)];
    end

    fprintf([erase bar '%s' extrastr], '%');
    drawnow;

end

如果你的 for 循环很大,并且每次传递都很短,它会增加大量的计算时间开销,所以只每 100 个循环调用它迭代,或者根据需要。

I made this function a while ago, it draws a nice ascii progress bar. Basically the same idea as the other two answers to your question, but a bit more packaged-up

function progressbar(percent, N, init, extrastr)
% Draws a progress bar in the matlab command prompt. Useful for lengthly
% calculations using for loops
%
% Arguments:
%   - percent:  A number between 0 and 1
%   - N:        how many characters wide the bar should be
%   - init:     (optional; default false) true or false; whether or not
%               this is the first time calling the progressbar function for
%               your current bar.
%   - extrastr: (optional; default char(10)) An extra string to append to
%               the progress bar. Things will go screwy at the command
%               console if this string changes length from call to call of
%               progressbar.
%
% Outputs:
%
% Usage Example:
%
%   for k=1:1000
%       progressbar(k/1000,50,k==1,sprintf('\n We are are on number%4d\n', k));
%       % fake a computation
%       pause(0.05);
%   end
%

    if nargin < 3
        init = 0;
    end
    if nargin < 4
        extrastr = char(10);
    end

    percent = min(max(real(percent),0),1);

    done = round(N*percent);
    done_str = '*'*ones(1, done);
    left_str = '-'*ones(1, N-done);
    bar = sprintf(['||' done_str left_str '|| %3d'],round(percent*100));

    erase = [];
    if ~init
        % use backspace characters to erase the previously drawn bar
        erase = ['' char(8)*ones(1,length(bar)+length(extrastr)+1)];
    end

    fprintf([erase bar '%s' extrastr], '%');
    drawnow;

end

If your for loop is enormous, and each pass is short, it will add a lot of overhead computation time, so only call it every 100 loop iterations, or as need be.

赢得她心 2025-01-14 10:40:56

您还可以使用 waitbar() 函数。虽然有点慢,但是看起来不错。

You can also use the waitbar() function. It is a bit slow, but looks nice.

回忆追雨的时光 2025-01-14 10:40:56

无论您要使用哪种风格的等待栏,我建议定义一个等待栏的接口并实现它。

 classdef IWaitBar
       methods(Abstract)
            GoToPos(positionPercent)
       end
 end

因此,计算函数和 GUI 绘图之间的耦合是松散的。

通过这种方式,您可以:

  • 更改任何 WaitBar 的实现,而无需修改计算函数。
  • 轻松添加更多 WaitBar,并按需切换它们
  • 如果您不想显示任何内容,请编写一个空的 WaitBar

No matter what style of waitbar you are going to use, I suggest defining an interface of waitbars, and implementing it.

 classdef IWaitBar
       methods(Abstract)
            GoToPos(positionPercent)
       end
 end

Thus, you get loose coupling between the function that calculates and the GUI drawing.

In this way you can:

  • Change the implementation of any WaitBar without modifying the calculating function.
  • Add more WaitBars easily, and switch them on demand
  • Write an empty WaitBar in cases you don't want to show anything.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文