How to track lengthy calculations?

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

Is it possible to configure this counter so that when it is updated on the screen, it replaces the previous value?

That is, printing the iterator variable in is fornot normal, because Matlab either prints it on a new line or after the previous value, but after 10,000 iterations the screen will be filled anyway. In addition, I would like to update the counter in each turn.

+5
source share
5 answers
fprintf('\n')
for i=1:15
    fprintf([repmat('\b', 1, length(num2str(i-1))) '%d'], i)
    pause(0.1)
end
fprintf('\n')
+5
source

You can use the \breverse space character to print. eg:.

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

, 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 , .

+5
+3

, , .

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

, , .

:

  • WaitBar .
  • WaitBar
  • Write blank WaitBarin cases when you do not want to show anything.
+3
source

All Articles