在MATLAB中输出文本/数字,但在命令窗口中覆盖相同的行 [英] Output text/numbers in MATLAB but overwrite same line in command window
问题描述
我该怎么做?我在其他一些程序中看到过,所以我知道这是可能的,但不是如何。
举个例子,让我们先说一下for循环的第一次迭代,我希望它能在命令提示符下输出:
>>测量1:0.33 0.23 0.34 -32.32
测量2:433.2
文字材料:'吃老鼠的猫'
现在,在循环的第二次迭代中,我不想要新的一行或多行,我只想将旧的数字和旧的文本替换成 ,在 same 放置在命令窗口中。所以在第二次迭代时,我可能会得到这样的结果:
>>测量1:-132.3 32.1 32.23 -320.32
测量2:3.2
文字材料:'吃猫的狗'
谢谢
以下是您正在寻找的示例:
%#生成数据
Measurement1 = {[0.33 0.23 0.34 -32.32]; [-132.3 32.1 32.23 -320.32]};
Measurement2 = {433.2; 3.2};
TextStuff = {'吃老鼠的猫'; 吃猫的狗;
s = cell2struct([Measurement1,Measurement2,TextStuff],...
{'Measurement1','Measurement2','TextStuff'},2);
$ b str_format = @(tag,value)sprintf('%s:%s',tag,value);
%#迭代数据并打印在同一数字上
figure
for i = 1:length(s)
%#清除这个数字
clf,set(gcf,'color','white'),axis off
%#输出数据
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))
%#等待直到用户按下一个键
pause
end
请注意, pause
强制您在下一次迭代执行前按下一个键。我已经把它放在那里,以便你有机会看到每个迭代的数字。
PS
基于这个答案(对于你的另一个问题),你也可以输出LaTex方程。
< hr>
编辑 - 更多解释:
cell2struct
是一个函数,一个单元阵列来构造数组。在你的情况下,你有 Measurement1
, Measurement2
和 TextStuff
,每个元素都是一个单元格数组,其中包含有关不同字段的数据。
所有单元格数组统一成一个单元格数组 [Measurement1,Measurement2, TextStuff] 。 cell2struct
从每个单元格数组中取出每一行并形成一个结构体,结果存储为一个结构体数组,如下所示:
$
$ struct $ array
Measurement1
Measurement2
TextStuff
您可以使用 我建议你在MATLAB命令提示符下键入 帮助函数 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: 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: Thanks Here's an example of what you're looking for: Note that P.S EDIT - some more explanation: You can extract the first set of values using I suggest you that type The helper function 这篇关于在MATLAB中输出文本/数字,但在命令窗口中覆盖相同的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! s(1)来提取第一组值/ code>,第二个使用
s(2)
,依此类推。
例如 s(1).TextStuff
给你'吃老鼠的猫'
/ p>
s
来查看它的内容。 b
str_format
是一个匿名函数,我已经创建了格式化每个字段的输出字符串。它的输入参数是标记
(字段名字符串)和值
(字段值字符串),它们连接在一起 sprintf
命令,类似于 sprintf
在C / C ++中的功能。>> Measurement1 : 0.33 0.23 0.34 -32.32
Measurement2 : 433.2
Text Stuff : 'The cat who ate the rat'
>> Measurement1 : -132.3 32.1 32.23 -320.32
Measurement2 : 3.2
Text Stuff : 'The dog who ate the cat'
%# 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
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.
Based on this answer (to another question of yours), you can also output LaTex equations.
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
s(1)
, the second using s(2)
, and so on.
For instance s(1).TextStuff
gives you 'The cat who ate the rat'
.s
in the MATLAB command prompt to see its contents.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++.