필터 지우기
필터 지우기

How do I overwrite text in the command window?

조회 수: 86 (최근 30일)
Kyle Poe
Kyle Poe 2017년 7월 10일
답변: Cristi Savlovschi 2021년 7월 15일
I am trying to write a counter that uses the format "processing subject ### of 123", where the ### changes in the command window upon every subject change. I know this could be accomplished in a loop with something like
fprintf('processing subject 000 of 123')
for number in 1:123
fprintf('\b\b\b\b\b\b\b\b\b\b%s of 123',subnum(number))
% subnum is a 3-digit number in character form to preserve the 3-digit format declared elsewhere
end
But that is ugly and terribly inefficient. How do I rectify the problem?

채택된 답변

Walter Roberson
Walter Roberson 2017년 7월 10일
That is the most efficient way. There is no way to address individual characters or pixels of the command output window (unless perhaps you can get at it from the java level.)
  댓글 수: 2
dpb
dpb 2017년 7월 10일
And, \r is interpreted same as \n so that doesn't help, either.
But, might consider either a waitbar or just a msgbox
Kyle Poe
Kyle Poe 2017년 7월 10일
Ah. Oh well. Thank you anyway!

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Eeshan Bhatt
Eeshan Bhatt 2018년 10월 25일
편집: Eeshan Bhatt 2018년 10월 25일
I know you have an accepted answer, but here's a quick robust code that I use often.
nbytes = fprintf('processing 0 of %d', length(z));
for nz = z
while nbytes > 0
fprintf('\b')
nbytes = nbytes - 1;
end
nbytes = fprintf('processing %d of %d', nz, length(z));
% YOUR PROCESS HERE
%
%
%
end
  댓글 수: 1
Ted Shultz
Ted Shultz 2019년 7월 19일
편집: Ted Shultz 2019년 7월 19일
Eeshan, this is a nice solution! I would propose this small modification so that it runs a little faster, and is a little simpler:
nbytes = fprintf('processing 0 of %d', length(z));
for nz = z
fprintf(repmat('\b',1,nbytes))
nbytes = fprintf('processing %d of %d\n', nz, length(z));
% YOUR PROCESS HERE
%
%
%
end

댓글을 달려면 로그인하십시오.


Fangjun Jiang
Fangjun Jiang 2017년 7월 10일
or go fancy, waitbar()
  댓글 수: 1
Kyle Poe
Kyle Poe 2017년 7월 11일
This is what I ended up using, thank you!

댓글을 달려면 로그인하십시오.


Boubat Matthieu
Boubat Matthieu 2020년 7월 28일
I packaged the use of fprintf('\b') 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:
prefix = 'processing subject ';
suffix = ' of 123';
progression = UpdatableText(prefix,suffix); % UpdatableText is the actual class
for i = 1:123
progression.print(num2str(i));
end
There are also methods to easily add a percent value and a progress bar.

Cristi Savlovschi
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;

카테고리

Help CenterFile Exchange에서 Dialog Boxes에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by