【问题标题】:How to monitor long calculations?如何监控长计算?
【发布时间】:2012-03-10 06:37:14
【问题描述】:

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

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

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

【问题讨论】:

    标签: user-interface matlab iteration printf


    【解决方案1】:
    fprintf('\n')
    for i=1:15
        fprintf([repmat('\b', 1, length(num2str(i-1))) '%d'], i)
        pause(0.1)
    end
    fprintf('\n')
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • ……但如果你数到 11,它就失败了!
      • @Simon:你应该把 10 的声音调大,然后把它放在首位。
      • 那是我,我没有得到“让 10 更响亮”。你能解释一下吗?
      • @Simon:这是一个Spinal Tap 参考。我假设扩展这种技术以支持更长的数字字符串对读者来说是一个简单的练习......
      • 我怎样才能以编程方式创建一个 n 退格的字符串数组?这不起作用:for i = 1:4 del(i) = '\b'; end.
      【解决方案3】:

      我前段时间做了这个函数,它绘制了一个漂亮的 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 次循环迭代或需要时调用它。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          无论你要使用哪种风格的waitbar,我都建议定义一个waitbars的接口,并实现它。

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

          因此,您在计算函数和 GUI 绘图之间获得了松散耦合。

          这样你可以:

          • 在不修改计算函数的情况下更改任何WaitBar的实现。
          • 轻松添加更多WaitBars,并按需切换
          • 写一个空的WaitBar,以防您不想显示任何内容。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多