在MATLAB中计算警告消息

时间:2014-12-14 12:45:59

标签: matlab error-handling warnings

我在循环中使用gmdistribution.fit来获取大约100,000个数据,每个数据都有250个样本。

当我运行脚本时,我收到警告,

Warning: Failed to converge in 100 iterations for gmdistribution
with 2 components 
> In @gmdistribution\private\gmcluster at 183
  In gmdistribution.fit at 174

有没有办法计算警告(类似catch),所以我可以知道其中有多少没有收敛?

这是如何计算警告消息的一般问题,GMM只是一个例子。

1 个答案:

答案 0 :(得分:1)

如果您使用的命令每次调用时只输出一个警告,您可以使用:

warningCounter = containers.Map();
while(youComputeStuff)
    functionThatGivesASingleWarning();
    [msgstr, msgid] = lastwarn;
    lastwarn(''); % Reset lastwarn
    if ~isempty(msgstr);
        if isKey(warningCounter, msgstr)
            warningCounter(msgstr) = warningCounter(msgstr)+1;
        else
            warningCounter(msgstr) = 1;
        end
    end
end
disp([warningCounter.keys; warningCounter.values])

如果你不能拥有这种控制流程,因为你的函数可以在每次调用时给出多个警告,你可以考虑覆盖内置的warning并使用一些全局计数变量。

相关问题