Effacer les filtres
Effacer les filtres

Output numbers, but WITHOUT a new line.

100 vues (au cours des 30 derniers jours)
Kalamaya
Kalamaya le 3 Sep 2012
Commenté : Sen Chen le 20 Juin 2018
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

Réponses (4)

Teja Muppirala
Teja Muppirala le 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 commentaires
Teja Muppirala
Teja Muppirala le 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
Sen Chen le 20 Juin 2018
I was using R2016a in nodisplay mode, still can't delete characters on previous lines.

Connectez-vous pour commenter.


Jan
Jan le 4 Sep 2012
Modifié(e) : Jan le 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.

Jan
Jan le 3 Sep 2012
Modifié(e) : Jan le 3 Sep 2012
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 commentaires
Jan
Jan le 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
Oleg Komarov le 4 Sep 2012
As Teja describes in his post I can delete line breaks and jump to previous lines. R2012a Winxp 32.

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 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.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by