如何在不打印变量的情况下在 Matlab 循环中监视变量?
How can you monitor a variable during a Matlab loop without printing it?
我是运行一个循环,在循环中计算变量。能够查看这些变量的当前值将很有用。打印它们没有用,因为循环的其他部分正在打印大量文本。此外,在 Workspace
选项卡上,直到循环结束才会显示值。
有没有办法监控这些变量,f.i。通过将它们打印成 window?
您可以使用 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
我是运行一个循环,在循环中计算变量。能够查看这些变量的当前值将很有用。打印它们没有用,因为循环的其他部分正在打印大量文本。此外,在 Workspace
选项卡上,直到循环结束才会显示值。
有没有办法监控这些变量,f.i。通过将它们打印成 window?
您可以使用 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