我正在运行一个循环,其中计算变量.能够看到这些变量的当前值是有用的.打印它们没有用,因为循环的其他部分正在打印大量文本.此外,在Workspace
选项卡上,值不会显示,直到循环结束.
有没有办法监控这些变量,通过将它们打印到窗口中?
您可以使用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