Output numbers, but WITHOUT a new line.
114 views (last 30 days)
Show older comments
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
0 Comments
Answers (4)
Teja Muppirala
on 4 Sep 2012
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 Comments
Teja Muppirala
on 4 Sep 2012
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
on 20 Jun 2018
I was using R2016a in nodisplay mode, still can't delete characters on previous lines.
Jan
on 4 Sep 2012
Edited: Jan
on 4 Sep 2012
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.
0 Comments
Jan
on 3 Sep 2012
Edited: Jan
on 3 Sep 2012
You can't do this in Matlab's command window.
A workaround would be to delete the command window by CLC and print the complete text again.
3 Comments
Jan
on 4 Sep 2012
@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
on 4 Sep 2012
As Teja describes in his post I can delete line breaks and jump to previous lines. R2012a Winxp 32.
Image Analyst
on 3 Sep 2012
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.
0 Comments
See Also
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!