您可以实现一个try/catch块来捕获(原始不是)错误消息并在链接确实断开时跳过图像。
当我们使用以下语法时:
try
A = imread('http://www.gossip.is/cgi-sys/suspendedpage.cgi');
catch ME
%// Just so we know what the identifier is.
ME
end
Matlab 首先尝试读取 url 给出的图像。如果不能,我们向它询问catch
错误消息(实际上是 MException)并执行一些其他适当的操作。
问题是,我们需要知道确切的错误消息是什么,以便在try/catch
块中识别它。
当我输入上面的代码时,我得到了以下结构ME
:
ME =
MException with properties:
identifier: 'MATLAB:imagesci:imread:fileFormat'
message: 'Unable to determine the file format.'
cause: {0x1 cell}
stack: [2x1 struct]
因此,一旦我们知道产生错误的确切标识符,我们就可以使用strcmp
它在try/catch
块中查找它。例如使用以下代码:
clear
clc
try
A = imread('http://www.gossip.is/cgi-sys/suspendedpage.cgi');
catch ME
if strcmp(ME.identifier,'MATLAB:imagesci:imread:fileFormat')
disp('Image link broken')
end
A = imread('peppers.png');
end
imshow(A);
Matlab 显示“图像链接断开”并按peppers.png
预期读取。
希望有帮助!