如何在不打印的情况下在Matlab循环中监视变量?

时间:2015-12-18 10:45:20

标签: matlab loops monitoring

我正在运行一个循环,其中计算变量。能够看到这些变量的当前值是有用的。打印它们没有用,因为循环的其他部分正在打印大量文本。此外,在Workspace选项卡上,这些值在循环结束前不会显示。

有没有办法监控这些变量,f.i。将它们打印到窗口中?

1 个答案:

答案 0 :(得分:6)

您可以使用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