Any way to get Matlab to execute a command, if a script runs into an error and terminates?
Show older comments
Is there a way to get Matlab to execute a command (e.g. send me an email), if it should run into an error and terminate when running some code?
Bonus: It would be helpful if this could be set up directly in the script, rather than being a Matlab setting as such, since because I don't want to notified by an mail every time an error occurs in Matlab - just when I am running certain long scripts.
This would be immensely helpful - since our analysis takes so long to run, we typically leave it running over-night/over the day, and come back at the end to check out the results. If I could set my script up so that Matlab sends me an email (I already know the email-sending bit) if it runs into an error, then I'd be able to remote-desktop in and sort it wherever I am.
3 Comments
Eleanor
on 18 Oct 2012
Image Analyst
on 18 Oct 2012
Why do you think some other construct is going to be simpler than try/catch???
Ben
on 7 Oct 2019
In case someone like me comes along and finds this... I was looking for something nicer and there is a function for cleaning up functions called onCleanup https://mathworks.com/help/matlab/matlab_prog/cleaning-up-when-the-function-completes.html
Accepted Answer
More Answers (2)
Image Analyst
on 18 Oct 2012
Edited: Image Analyst
on 18 Oct 2012
Yes. Just use try catch:
try
% your script which can fail....
catch ME
% An error will put you here.
errorMessage = sprintf('Error in myScrip.m.\nThe error reported by MATLAB is:\n\n%s', ME.message);
uiwait(warndlg(errorMessage));
send_email(errorMessage);
% Run other code or use the system() command to run other programs...
end
Let me know if you need demo code for how to send email, but there is demo code in the help for sendmail() and setpref().
% Here's where we actually send out the e-mail with the file attached.
sendmail(recipientsEMail, subjectLine, messageBody, attachedFullFileName)
Sean de Wolski
on 18 Oct 2012
Edited: Sean de Wolski
on 18 Oct 2012
0 votes
- Nope, you'll need to use try/catch.
- Use sendmail or something like this http://www.mathworks.com/matlabcentral/fileexchange/28733-notifier (a wrapper for sendmail)
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!