Deleting the last displayed line (or part of it) in the command window.

302 ビュー (過去 30 日間)
Mohamed Abdalmoaty
Mohamed Abdalmoaty 2017 年 2 月 1 日
編集済み: Amin Kassab-Bachi 2021 年 8 月 18 日
I'm trying to use the same line in the command window of Matlab to print the progress of my code.
I have read several solutions and currently I'm using the following command:
disp([repmat(char(8), 1, num_of_Char_to_del)])
This works fine on my Matlab (9.1.0.441655 (R2016b)) when running on Windows 7.
For example when I have the following code
disp('Hello World')
pause
disp([repmat(char(8), 1, 6)])
in an m file with the name 'test.m', and I run the file, first I get
>> test
Hello World
then pressing any key I get
>> test
Hello
>>
Which is what I want.
However, trying the same exact thing on a Matlab (8.5.0.197613 (R2015a) ) running on Linux server, first I get
>> test
Hello World
and if I press any key I get
>> test
Hello World
>>
which is not what I expect.
Can anyone explain what is the issue?
  2 件のコメント
David J. Mack
David J. Mack 2017 年 2 月 1 日
My first guess would be EOL encoding (\r\n vs. \n). Did you try using fprintf instead of disp (which implicitly adds an EOL). So, e.g.
fprintf('Hello World\n');
pause
fprintf(repmat('\b', 1, 6)); %Deletes 'world' and '\n
Greetings, David
Mohamed Abdalmoaty
Mohamed Abdalmoaty 2017 年 2 月 1 日
Thanks for your comment. Yes, I have tried fprintf but I could not make it work.
I tried to run your code (on the version running on linux server) and it is not doing what we expect. First I get
>> test
Hello World
but when I press any key I get
>> test
Hello World
>>

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

回答 (3 件)

Piotr
Piotr 2019 年 10 月 15 日
Hello,
here is simple sollution, small upgrade of Jan's answer, without checking the length of the line that you want to clear:
lineLength = fprintf('It is %2.1f degrees outside',21.1);
pause(2)
fprintf(repmat('\b',1,lineLength))
Cheers,
Piotr

Jan
Jan 2017 年 2 月 1 日
Try it without the line break:
fprintf('Hello World');
pause
fprintf(repmat('\b', 1, 6));
How did you open Matlab on the server? Through a text terminal or with the GUI?
  4 件のコメント
Mohamed Abdalmoaty
Mohamed Abdalmoaty 2017 年 2 月 1 日
編集済み: Mohamed Abdalmoaty 2017 年 2 月 1 日
There are only two options for "The Backspace Key". Changing this option does not change anything in the result of the code. I also disabled the AutoWrap which is on by default, however again with no effect. (I'm attaching my original settings).
sambhav jain
sambhav jain 2020 年 4 月 30 日
hello
i have been running matlab online . In my case it simply prints in next line . the backspace one is not working

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


Amin Kassab-Bachi
Amin Kassab-Bachi 2021 年 8 月 18 日
編集済み: Amin Kassab-Bachi 2021 年 8 月 18 日
If you're trying this from within the command line
msg=fprintf("Anything I want to write\n");
pause
fprintf(repmat('\b', 1, (msg+39))); % covers the length of the message + length of the backspace command.
Otherwise, the answers above should work.

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by