Output text/numbers in MATLAB but overwrite same line in command window

♀尐吖头ヾ 提交于 2019-12-12 12:26:35

问题


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 prompt:

>> 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


回答1:


Here's an example of what you're looking for:

%# Generate the data
Measurement1 = {[0.33 0.23 0.34 -32.32]; [-132.3 32.1 32.23 -320.32]};
Measurement2 = {433.2; 3.2};
TextStuff = {'The cat who ate the rat'; 'The dog who ate the cat'};
s = cell2struct([Measurement1, Measurement2, TextStuff], ...
    {'Measurement1', 'Measurement2', 'TextStuff'}, 2); 

str_format = @(tag, value)sprintf('%s:%s', tag, value);

%# Iterate over the data and print it on the same figure
figure
for i = 1:length(s)

    %# Clear the figure
    clf, set(gcf, 'color', 'white'), axis off

    %# Output the data
    text(0, 1, str_format('Measurement1', num2str(s(i).Measurement1)));
    text(0, 0.9, str_format('Measurement2', num2str(s(i).Measurement2)));
    text(0, 0.8, str_format('TextStuff', s(i).TextStuff))

    %# Wait until the uses press a key
    pause
end

Note that pause forces you to press a key before the next iteration is executed. I've put it there so that you can get the chance see the figure in each iteration.

P.S
Based on this answer (to another question of yours), you can also output LaTex equations.


EDIT - some more explanation:

cell2struct is a function that converts a cell array to structure array. In your case, you have Measurement1, Measurement2 and TextStuff, each being a cell array holding data about different fields.
All cell arrays are unified into one array of cell arrays: [Measurement1, Measurement2, TextStuff]. cell2struct takes each row from each cell array and forms a struct, the result being stored as an array of structs, like so:

s = 

2x1 struct array with fields:
    Measurement1
    Measurement2
    TextStuff

You can extract the first set of values using s(1), the second using s(2), and so on. For instance s(1).TextStuff gives you 'The cat who ate the rat'.

I suggest you that type s in the MATLAB command prompt to see its contents.

The helper function str_format is an anonymous function that I've created to format the output string for each field. Its input arguments are tag (the field name string) and value (field value string), which are concatenated together using the sprintf command, similar to the sprintf function in C/C++.




回答2:


This article indicates that you can do it with backspace, though it also seems to say that it won't work with multiple lines.

The principle is that at each iteration you output enough backspace characters to move the cursor to the beginning of your output and then start writing your new output over the old. You'll have to keep track of the cursor position as you move it back and forth.




回答3:


I use 'dispstat' function just for this purpose. It can update the previous output which is a missing function of default 'disp'. Very simple to use. It can be downloaded from here: http://www.mathworks.com/matlabcentral/fileexchange/44673-overwritable-message-outputs-to-commandline-window

***Sample usage:

 dispstat('','init'); % One time only initialization
 dispstat(sprintf('Begining the process...'),'keepthis','timestamp');
 for i = 97:100
     dispstat(sprintf('Progress %d%%',i),'timestamp');
     %doing some heavy stuff here
 end
 dispstat('Finished.','keepprev');

***Output:

11:25:37 Begining the process...
11:25:37 Progress 100%
Finished.

All the best



来源:https://stackoverflow.com/questions/12249579/output-text-numbers-in-matlab-but-overwrite-same-line-in-command-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!