MATLAB Answers


Any way to get Matlab to execute a command, if a script runs into an error and terminates?

Asked by Eleanor
on 18 Oct 2012
Latest activity Commented on by Ben
on 7 Oct 2019 at 16:48
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.


Even bigger bonus: Preferably this wouldn't involve just sticking everything into a try/catch function. Just because I've got several of those already, so I'm hoping to minimize how tangled up and difficult-to-read everything gets.
Why do you think some other construct is going to be simpler than try/catch???
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

Sign in to comment.


3 Answers

Answer by Jason Ross
on 18 Oct 2012
Edited by Jason Ross
on 27 Nov 2018
 Accepted Answer

I largely concur with the try/catch approach already suggested by others. There is functionality built into the Parallel Computing Toolbox and MATLAB Job Server that will let you set callbacks for the state of your job, and also functionality built into the desktop to monitor job progress.
You could, for example, write a callback function that would check the state of your job when it completed and email you if an error was encountered while the job was running.
This would requre that you set up a cluster and use the Parallel Computing Toolbox to make it work -- which may or may not be something you have access to or want to get into if the already suggested solutions will meet your needs.
The following three links provide a basic sketch of what you would need to do -- establish the callback (the code that would send mail), how to submit your script to the cluster (batch), and then the properties you would query in your callback (job state is failed).
The job monitor allows you to monitor activity on your cluster in a GUI:


That sounds good - I will see if I can do that. Thanks!
Great! I'd recommend that as you test things out, you might want to start with some trivial examples of scripts and functions that error out to see if it does what you are looking for -- just call the error() function and you can generate an error, so you can see how the callbacks will work rather than waiting for errors from your analysis code.
Also, if you need help with the setup of MDCS you can call install support and they should be able to get you set up.

Sign in to comment.

Answer by Image Analyst
on 18 Oct 2012
Edited by Image Analyst
on 18 Oct 2012

Yes. Just use try catch:
% 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);
% Run other code or use the system() command to run other programs...
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)

  1 Comment

Yep no worries, I know how to do it this way. Was just hoping/wishfully-thinking that someone might have figured out a neater way to do it! Thanks :)

Sign in to comment.

Answer by Sean de Wolski
on 18 Oct 2012
Edited by Sean de Wolski
on 18 Oct 2012

  1. Nope, you'll need to use try/catch.
  2. Use sendmail or something like this (a wrapper for sendmail)


Sign in to comment.