Throw Error in TimerFcn

10 views (last 30 days)
Sohil Shrestha
Sohil Shrestha on 8 Apr 2020
Commented: Sohil Shrestha on 8 Apr 2020
I want to throw an error when timer fires up .
try
timeout = timer('TimerFcn','error(''TIMEOUT OCCURED'');','StartDelay',0.01);
start(timeout);
'''Some Code'''
stop(timeout);
delete(timeout);
catch ME
delete(timeout);
disp('Error is caught')
''' Do something else '''
end
Upon executing, I am getting
Error while evaluating TimerFcn for timer 'timer-47'

Accepted Answer

Steven Lord
Steven Lord on 8 Apr 2020
Edited: Steven Lord on 8 Apr 2020
This is not going to work as you've written it.
Imagine if the timer's StartDelay was 60 seconds and the body of your try block took 5 seconds to run. Would MATLAB wait, twiddling its thumbs, for 55 seconds for the timer's delay to elapse and then jump into the catch block? No, it wouldn't. By the time the TimerFcn actually executes, the code that created and started the timer may have long since finished.
You could potentially get something like this to work as long as you're willing to have "Some Code" poll. Have the TimerFcn change a value stored somewhere accessible to the function (in the timer object's UserData, for example) and have "Some Code" periodically check if that stored value has changed. If it has, the body of the try has run for too long and "Some Code" should terminate.
Alternately, if you're willing to poll, you don't need a timer. Store the current time (as a datetime would be easist to check) at the start of "Some Code" and periodically check if the difference between the current time and stored time, terminating "Some Code" if it's been too long.
currentTime = datetime('now');
totalPause = 0;
while true
r = rand;
disp("About to pause for " + r + " seconds.")
pause(r)
totalPause = totalPause + r;
if datetime('now')-currentTime > seconds(4.5)
break;
end
end
end
disp("Paused for a total of " + totalPause + " seconds.")
  1 Comment
Sohil Shrestha
Sohil Shrestha on 8 Apr 2020
Based on the documentation of stop and delete, timer object's Running property is set to false and and it is deleted from the memory. If the try block code is executed in less than 5 seconds, timer object will be deleted .
While the suggestion of polling is great, unfortunately I cannot use it since I am checking if a Simulink model is being compiled or not(which based on its size and computing resources may take milliseconds to couple of minutes). Based on my usecase, I have found Keyboard Interrupt is the straightforward way . The following code works for me .
timeout = timer('TimerFcn',' com.mathworks.mde.cmdwin.CmdWinMLIF.getInstance().processKeyFromC(2,67,''C'')','StartDelay',120);

Sign in to comment.

More Answers (1)

Ameer Hamza
Ameer Hamza on 8 Apr 2020
TimerFcn can either be a function handle or the name of a function. To display anything, you need to use a function. For example
timeout = timer('TimerFcn',@(~,~) disp('error(''This is error'');'),'StartDelay',0.01);
  3 Comments
Ameer Hamza
Ameer Hamza on 8 Apr 2020
Ok. I get your point. But the example you referred is not related to error handling. Can you tell why do you want to do such a thing? Isn't it logical to write your TimerFcn callback such that it executes the code inside 'catch' block?
Sohil Shrestha
Sohil Shrestha on 8 Apr 2020
I have updated the code above. The main purpose is for timeout functionality. I have a script that checks if a Simulink models compiles or not. If the model takes too long to compile, I basically want to interrupt and continue. hence throwing error.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!