필터 지우기
필터 지우기

Alert sound/mail for warning me if the program runs overtime.

조회 수: 3 (최근 30일)
samudra s
samudra s 2017년 1월 31일
댓글: Walter Roberson 2017년 2월 6일
Is there any way to notify me if a function I call doesn't return a result in 20seconds. (I am not allowed to make any change to the function. I can change only the main function which calls the function.)
Is it possible to run two codes parallel; one the function and the other a timer.
Thank you.

답변 (2개)

Walter Roberson
Walter Roberson 2017년 1월 31일
Yes, barely. You can use parfeval() to start the routine. After 20 seconds, check the status of the "future" that it returns; if it has not finished then cancel the future, and otherwise collect its result.
Note: any routine you run this way will not have access to graphics.

dbmn
dbmn 2017년 1월 31일
Another option is to use the timer function. But it is kind of tricky and not as nice.
clear all; close all; clc
% Initialize variables
finished_1 = false;
finished_2 = false;
% Run first Function
disp('FUNCTION 1')
start(timer('TimerFcn',{@checkfun, 'finished_1'},'StartDelay',1.5));
finished_1 = myfun1();
pause(1)
% Run second Function
start(timer('TimerFcn',{@checkfun, 'finished_2'},'StartDelay',1.5));
disp('FUNCTION 2')
finished_2 = myfun2();
%%Function declarations
function [finished] = myfun1()
pause(1);
finished = true;
end
function [finished] = myfun2()
pause(2);
finished = true;
end
function [] = checkfun(~, ~, varname)
finished = evalin('base',varname); % if you pass a variable it will take one without delay
if finished
disp('finished')
else
disp('not finished')
end
end
  댓글 수: 3
dbmn
dbmn 2017년 2월 6일
You are right. Thank you for clarifying the points of interuptions of the timer function - i didn't know that.
There is one way to cancel the execution if it goes on for too long - but it rather nasty by introducing try/catch statements with exceptions.
finished_1 = false;
try
start(timer('TimerFcn',{@checkfun, 'finished_1'},'StartDelay',1.5));
finished_1 = myfun1();
catch
% continue
end
while trowing an exception in the checkfun of the timer, if the execution takes too long.
function [] = checkfun(~, ~, varname)
finished = evalin('base',varname);
if ~ finished
error('my special error')
end
end
not nice, but lets you stop the current execution and resume with the rest of the code
Walter Roberson
Walter Roberson 2017년 2월 6일
The timer lives in a different execution context, just like graphics interrupts do. You cannot catch errors generated in callbacks by any routine outside the callback.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by