フィルターのクリア

Is it possible to rewrite/update a printed line?

200 ビュー (過去 30 日間)
Mr M.
Mr M. 2017 年 12 月 12 日
コメント済み: Omar Alamoudi 2022 年 1 月 14 日
I want to simulate a progress bar during the run, so 1% has to be updated to 2% for example
  2 件のコメント
per isakson
per isakson 2017 年 12 月 13 日
編集済み: per isakson 2017 年 12 月 19 日
Yes! The trick is to use backspace, \b, in the print statement. Search the FEX for progress text
Mr M.
Mr M. 2017 年 12 月 18 日
Thanks, but progress bar was just an example. I want to know the answer to the original question: update a printed line. I've tried to find the answer in the above linked textprogressbar functions, but I still cannot figure out where and how lines are updated in those codes!?

サインインしてコメントする。

採用された回答

Steven Lord
Steven Lord 2017 年 12 月 13 日
You may be able to do this, but you may instead want to use the waitbar function included in MATLAB.
  1 件のコメント
Omar Alamoudi
Omar Alamoudi 2022 年 1 月 14 日
Warning: as effective as waitbar might be for the task in question, if the update is too frequent for the waitbar, the calculations are significanly slowed down.

サインインしてコメントする。

その他の回答 (3 件)

Valerio Pampanoni
Valerio Pampanoni 2019 年 1 月 25 日
編集済み: Valerio Pampanoni 2019 年 1 月 25 日
I know it is a little late, but I asked myself the same question while editing a very large code and I found my own solution which involves using the fprintf function and the backspace character '\b'. I tried to use the waitbar function, but it slows down the code so much it is not viable at all.
imax = 100;
prog = 0;
fprintf(1,'Computation Progress: %3d%%\n',prog);
for k = 1:1:imax
prog = ( 100*(k/imax) );
fprintf(1,'\b\b\b\b%3.0f%%',prog); pause(0.1); % Deleting 4 characters (The three digits and the % symbol)
end
fprintf('\n'); % To go to a new line after reaching 100% progress
Note how I need to use 4 backspace characters to delete the previous progress and two percent characters to write the percent symbol at the end of the line. But the most important part is the fact that you must use the handle on the fprintf function to update the previous statement.
Works on Matlab 2018b Update 2.
Edit: rather than using the floor function, I simply display the first three digits of the floating point number 'prog', saving a little computation time.
  1 件のコメント
Dominik Braun
Dominik Braun 2021 年 2 月 5 日
Thanks, that was very helpful

サインインしてコメントする。


Cristi Savlovschi
Cristi Savlovschi 2021 年 7 月 15 日
編集済み: Cristi Savlovschi 2021 年 7 月 15 日
function dispProgressMsg(msg)
ASCII_BKSP_CHAR = 8;
persistent prevMsgLen;
if isempty(prevMsgLen)
prevMsgLen = 0;
end
disp([ char(repmat(ASCII_BKSP_CHAR,1,prevMsgLen)) msg]);
prevMsgLen = numel(msg)+1;

Boubat Matthieu
Boubat Matthieu 2020 年 7 月 28 日
I packaged the use of fprintf('\b'), which delete the last character in the command window, into a class hoping that it will ease the way to rewrite/update text in the command window. If you want to give it a try it's available here.
It goes like this:
progression = UpdatableText; % UpdatableText is the actual class
for i = 1:100
progression.print(sprintf('step %d/100',i));
end
There are also methods to easily add a percent value and a progress bar.

カテゴリ

Help Center および File ExchangeDialog Boxes についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by