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

28 visualizaciones (últimos 30 días)
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 comentarios
Image Analyst
Image Analyst el 18 de Oct. de 2012
Why do you think some other construct is going to be simpler than try/catch???
Ben
Ben el 7 de Oct. de 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

Iniciar sesión para comentar.

Respuesta aceptada

Jason Ross
Jason Ross el 18 de Oct. de 2012
Editada: Jason Ross el 27 de Nov. de 2018
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:
  3 comentarios
Jason Ross
Jason Ross el 22 de Oct. de 2012
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.
Xingwang Yong
Xingwang Yong el 12 de Dic. de 2020
The callback method is a little bit limited, as this documentation says, " The callback properties are available only when using a MATLAB Job Scheduler cluster."

Iniciar sesión para comentar.

Más respuestas (2)

Image Analyst
Image Analyst el 18 de Oct. de 2012
Editada: Image Analyst el 18 de Oct. de 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)
  1 comentario
Eleanor
Eleanor el 18 de Oct. de 2012
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 :)

Iniciar sesión para comentar.


Sean de Wolski
Sean de Wolski el 18 de Oct. de 2012
Editada: Sean de Wolski el 18 de Oct. de 2012
  1. Nope, you'll need to use try/catch.
  2. Use sendmail or something like this http://www.mathworks.com/matlabcentral/fileexchange/28733-notifier (a wrapper for sendmail)

Categorías

Más información sobre Downloads en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by