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

标签 matlab loops monitoring

我正在运行一个循环,其中计算变量。能够查看这些变量的当前值将很有用。打印它们没有用,因为循环的其他部分正在打印大量文本。此外,在 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

关于matlab - 如何在不打印变量的情况下在 Matlab 循环中监视变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34353787/

相关文章:

postgresql - 如何使用 Zabbix 通过简单的无代理检查来配置数据库服务状态

tomcat - cpu如何分配给一个进程?

matlab - 如何求解这个受约束的正值非线性方程组?

string - 在单元格数组中所有字符串的每个字符之间放置一个字符

python - 在 Pythonic while 循环中执行迭代名称/变量的替代方法

php - 循环读取文件的每一行到它自己的数组

security - 如何以编程方式检测可用的ssh身份验证类型?

matlab - 为什么 Matlab 不使用 Swap 而是错误 "Out of memory"?

matlab - 在 MATLAB 上创建正方形网格时如何去除 'fishbowl' 效果?

php - 通过foreach循环没有得到预期的结果