3

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

4

3 回答 3

2

我只是为了这个目的使用'dispstat'函数。它可以更新以前的输出,这是默认“disp”的缺失功能。使用非常简单。它可以从这里下载:http: //www.mathworks.com/matlabcentral/fileexchange/44673-overwritable-message-outputs-to-commandline-window

***示例用法:

 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');

***输出:

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

一切顺利

于 2013-12-12T10:22:50.613 回答
2

This article表明您可以使用退格键来执行此操作,尽管它似乎也说它不适用于多行。

原则是在每次迭代中输出足够的退格字符以将光标移动到输出的开头,然后开始在旧输出上写入新输出。当您来回移动光标时,您必须跟踪光标位置。

于 2012-09-03T15:58:06.803 回答
1

这是您正在寻找的示例:

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

请注意,这会pause强制您在执行下一次迭代之前按下一个键。我把它放在那里,这样你就有机会在每次迭代中看到这个数字。

PS
基于this answer(对您的另一个问题),您还可以输出LaTex方程。


编辑- 更多解释:

cell2struct是将元胞数组转换为结构体数组的函数。在您的情况下,您有和Measurement1,每个都是一个单元格数组,其中包含有关不同字段的数据。 所有元胞数组都统一为一个元胞数组数组:。从每个单元格数组中获取每一行并形成一个结构,结果存储为一个 structs 数组,如下所示:Measurement2TextStuff
[Measurement1, Measurement2, TextStuff]cell2struct

s = 

2x1 struct array with fields:
    Measurement1
    Measurement2
    TextStuff

您可以使用 提取第一组值s(1),使用 提取第二组值,s(2)依此类推。比如s(1).TextStuff给你'The cat who ate the rat'

我建议您s在 MATLAB 命令提示符中键入以查看其内容。

辅助函数是我创建str_format的一个匿名函数,用于格式化每个字段的输出字符串。它的输入参数是tag(字段名称字符串)和value(字段值字符串),使用sprintf命令将它们连接在一起,类似于sprintfC/C++中的函数。

于 2012-09-03T15:56:24.383 回答