10

I've got a Matlab function that takes some time to run, and I'd like to show the user that progress is being made. Just disping the progress every 5% or so would clutter the screen too much, as the previous text would not be erased.

How can this problem be solved? There's other important information in the command window, so clearing it is out of the question.

4 Answers 4

19

Showing the progess in the Command Window is also possible (and maybe easier). I found a very simple, fast to implement solution on http://undocumentedmatlab.com/blog/command-window-text-manipulation/.

reverseStr = '';
for idx = 1 : someLargeNumber

   % Do some computation here...

   % Display the progress
   percentDone = 100 * idx / someLargeNumber;
   msg = sprintf('Percent done: %3.1f', percentDone); %Don't forget this semicolon
   fprintf([reverseStr, msg]);
   reverseStr = repmat(sprintf('\b'), 1, length(msg));
end

If you embedd this code the command line is showing (for example): "Percent done: 27.8" without entering a newline every iteration!

1
  • Note: this doesn't work on Octave, where standard output is buffered. Feb 3, 2017 at 10:56
8

You can use waitbar function for that. See MATLAB Documentation on waitbar.

1
  • I was looking for a text version, but that was only because I didn't know how useful waitbar was. Thanks!
    – Andreas
    Jun 15, 2012 at 12:48
0

Basically what is written by @Ergodicity is correct, just for Octave if you set the standard output to be buffered (which is default btw), you had to enable it by page_output_immediately(1); see this page for more octave doc: Terminal output

a very brief modifications on the proposed code:

reverseStr = '';
fprintf('Percent done: ');
for idx = 1 : someLargeNumber
   % Do some computation here...
   % Display the progress
   percentDone = 100 * idx / someLargeNumber;
   msg = sprintf('%3.1f', percentDone); %Don't forget this semicolon
   fprintf([reverseStr, msg]);
   reverseStr = repmat(sprintf('\b'), 1, length(msg));
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.