我正在运行一个循环,其中计算变量。能够查看这些变量的当前值将很有用。打印它们没有用,因为循环的其他部分正在打印大量文本。此外,在 Workspace
选项卡上,直到循环结束才会显示值。
有没有办法监控这些变量,f.i.通过将它们打印到窗口中?
您可以使用 text
创建一个图形对象并根据所需变量更新其 'string'
属性:
h = text(.5, .5, ''); %// create text object
for n = 1:1000
v = n^2; %// loop computations here. Variable `v` is to be displayed
set(h, 'string', ['Value: ' num2str(v)]);
drawnow %// you may need this for immediate updating
end
为了提高速度,您可以只更新每 S
次迭代:
h = text(.5, .5, ''); %// create text object
S = 10; %// update period
for n = 1:1000
v = n^2; %// loop computations here. Variable `v` is to be displayed
if ~mod(n,S) %// update only at iterations S, 2*S, 3*S, ...
set(h, 'string', ['Value: ' num2str(v)]);
drawnow %// you may need this for immediate updating
end
end
或使用 drawnow('limitrate')
,如@Edric 所述:
h = text(.5, .5, ''); %// create text object
for n = 1:1000
v = n^2; %// loop computations here. Variable `v` is to be displayed
set(h, 'string', ['Value: ' num2str(v)]);
drawnow('limitrate')
end
我是一名优秀的程序员,十分优秀!