Matlab - 如何捕获cp2tform函数发布的警告

时间:2014-06-27 10:58:49

标签: matlab error-handling warnings suppress-warnings

我有一组N个2D点集,我想解析这些N个集合,以便用另一个集{{找到仿射变换(平移,旋转,缩放,包括反射) 2D点的1}}。

为了做到这一点,我正在应用Matlab函数q。但是,在某些情况下,该功能会给我一个类似于下图所示的警告:

cp2tform

在这些情况下,使用Warning: The condition number of A is 117632159740.8394. > In maketform>validate_matrix at 328 In maketform>affine at 163 In maketform at 129 In cp2tform>findAffineTransform at 265 In cp2tform at 168 函数标识的变换矩阵不适用于2组2D点之间的实际变换。我怎样才能抓住这些情况才能跳过它们?我应该引入什么样的matlab函数或代码才能捕获这些情况以便能够跳过或处理它们?

2 个答案:

答案 0 :(得分:3)

处理警告:

正如here所述,您可以将特定警告转换为错误,并将其捕获到try / catch块中。

以下是如何处理特定警告(反转近似奇异矩阵)的示例:

% turn this specific warning into an error
s = warning('error', 'MATLAB:nearlySingularMatrix'); %#ok<CTPCT>

% invoke code trapping errors
try
    A = [1 2 3; 4 5 6; 7 8 9];
    B = inv(A);
    disp(B)
catch ME
    if strcmp(ME.identifier, 'MATLAB:nearlySingularMatrix')
        fprintf('Warning trapped: %s\n', ME.message);
    else
        rethrow(ME);
    end
end

% restore warning state
warning(s);

抑制警告:

当然,如果您想要取消警告消息,您可以使用以下方法查询最后发出的警告:

[msgstr, msgid] = lastwarn;

(或使用@Benoit_11显示的语法),然后将其关闭,直到暂时进入您的函数:

% turn it off
s = warning('off', msgid);

% ...

% restore state
warning(s);

答案 1 :(得分:1)

它可能无法完全解决您的问题,但您可以尝试以下方法:

您可以获得有关Matlab发布的警告的信息;例如,弹出此行的最后一个警告(取自matlab帮助):

w = warning('query','last')

你会得到这个:

w = 

    identifier: 'MATLAB:rmpath:DirNotFound'
         state: 'on'

然后你可以使用这个非常标识符来捕获matlab发出相同警告的其他实例,并可能跳过它们。我认为语法非常灵活。希望有所帮助!