忽略计时器 MATLAB 中的错误

Ignoring error in timer MATLAB

我有一个代码可以实时处理来自网络摄像头的视频,同时对其执行一些操作,读取每一帧。

为此,我使用函数 "timer"。有时,出于奇怪的原因,我会收到如下错误:

为定时器计算 TimerFcn 时出错 'timer-77'

下标索引必须是实数正整数或逻辑数。

有没有办法忽略这个错误并继续下一帧?

 function DetectTarget2()
    clc;imaqreset;close all;
try
    % For linux
    Vid = videoinput('linuxvideo', 1);
catch
    try
        % For mac
        Vid = videoinput('macvideo', 1);
    catch
        errordlg('No webcam available');
    end
end

set(Vid,'FramesPerTrigger',1);  %capture 1 frame every time Vid is triggered
set(Vid,'TriggerRepeat',Inf);   %infinite amount of triggers
set(Vid,'ReturnedColorSpace','RGB');
triggerconfig(Vid, 'Manual');   %trigger Vid manually within program

t = timer('TimerFcn',@dispim, 'Period', 0.04,...
    'executionMode','fixedRate');

function dispim(~,~)
        trigger(Vid)%trigger Vid to capture image
        im=getdata(Vid,1);
        detector = vision.CascadeObjectDetector('Cascade1Matlab.xml');
        bbox = step(detector, im); 

% CALCULATIONS

degrees=result;
end
end

您可以通过将代码包装在 try-catch 块中来避免因错误而停止:

function dispim(~,~)
    try
      %# code goes here
    catch me
      %# you get here if an error happens
      %# use the catch-block to make sure subsequent iterations will run fine
      disp(me.message); %# display the error message
    end
end