2015-02-09 69 views
0

我有一個代碼,它可以從網絡攝像頭實時處理視頻,同時對它執行一些操作,讀取每一幀。在計時器中忽略錯誤MATLAB

爲此,我使用函數「timer」。有時候,和奇怪的原因,我得到一個錯誤,如:

錯誤而評估TimerFcn定時器「定時器-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 

回答

1

可避免包裝代碼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