Output numbers, but WITHOUT a new line.

116 ビュー (過去 30 日間)
Kalamaya
Kalamaya 2012 年 9 月 3 日
コメント済み: Sen Chen 2018 年 6 月 20 日
So I have a for-loop, and at every iteration, I would like to display formatted text, along with some numbers. Normally one could use disp or fprintf I suppose, but what I want to do, is have the same part of the command window output the text/numbers, just overwriting the old output.
How might I be able to do that? I have seen it in some other programs so I know it is possible, but not how.
As an example, lets say on the first iteration of a for-loop, I want this to be output on the command window:
>> Measurement1 : 0.33 0.23 0.34 -32.32
Measurement2 : 433.2
Text Stuff : 'The cat who ate the rat'
Now, on the second iteration of the loop, I DONT want a new line or lines, I simply want the old numbers and old text to be replaced, in the same place on the command window. So on teh second iteration, I might get this:
>> Measurement1 : -132.3 32.1 32.23 -320.32
Measurement2 : 3.2
Text Stuff : 'The dog who ate the cat'
Thanks

回答 (4 件)

Teja Muppirala
Teja Muppirala 2012 年 9 月 4 日
What you describe is not entirely impossible,
tic
while toc < 3
S = sprintf('x = %f\ny = %f',rand,rand);
fprintf(S);
pause(0.2);
fprintf(repmat('\b',1,numel(S)));
end
... but... it does look kind of weird. You'll certainly want to hide the function browser (the fx next to the command window). Some of the other suggestions like using CLC or a GUI may be better options.
  3 件のコメント
Teja Muppirala
Teja Muppirala 2012 年 9 月 4 日
Hello Jan, I checked as far back as 2007a on my Windows machine, and \b is able to delete characters on previous lines as well. I assume there must be some other minor difference in environment between our PCs.
Sen Chen
Sen Chen 2018 年 6 月 20 日
I was using R2016a in nodisplay mode, still can't delete characters on previous lines.

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


Jan
Jan 2012 年 9 月 4 日
編集済み: Jan 2012 年 9 月 4 日
You can get the text of the command window using undocumented commands:
cmdWinDoc = com.mathworks.mde.cmdwin.CmdWinDocument.getInstance;
jString = cmdWinDoc.getText(cmdWinDoc.getStartPosition.getOffset, ...
cmdWinDoc.getLength);
String = char(jString);
It should be possible to modify the contents also, but my trials let Matlab crash. The corresponding mthods of cmdWinDoc are:
insertString(int, java.lang.String, javax.swing.text.AttributeSet)
replace(int, int, java.lang.String, javax.swing.text.AttributeSet)
Good luck - please save all your work before trying this and even better: do not start experiments on productive maschines.
We should ask Yair.
Anyhow, I'd prefer a GUI and keep the command window for dull status messages only.

Jan
Jan 2012 年 9 月 3 日
編集済み: Jan 2012 年 9 月 3 日
You can't do this in Matlab's command window.
You can delete characters in the current line, but not in lines before. See FEX: tprogress.
A workaround would be to delete the command window by CLC and print the complete text again.
  3 件のコメント
Jan
Jan 2012 年 9 月 4 日
@Oleg: Under my Windows system \b deleted characters until the first column only, but it does not remove former line breaks, such that only the current line can be accessed.
Oleg Komarov
Oleg Komarov 2012 年 9 月 4 日
As Teja describes in his post I can delete line breaks and jump to previous lines. R2012a Winxp 32.

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


Image Analyst
Image Analyst 2012 年 9 月 3 日
Jan has a good workaround using clc. Another workaround, and one that would give a more professional look to your app, is to use a GUI and write that string to a static text control on your GUI.
yourString = sprintf('Measurement1 : %.2f %.2f %.2f %.2f\nMeasurement2 : %.2f\n
Text Stuff : %s', m1(1), m1(2), m1(3), m1(4), m2, textStuff);
set(handles.handleToTextControl, 'String', yourString);
See the Mathwork's Doug Hull's Blog on video tutorials for info on how to create GUIs. You're eventually going to be creating GUIs anyway as you progress in learning MATLAB. This is a good reason to start learning.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by