Dynamic Code Completion Notifications through Emails and/or Text Messages
matlab example
Written by Daniel Herber on October 28, 2014.
Does your code take a long time to complete or is it being run on a machine different than your primary? During these situations, you might find an email and/or text notification of the status of the code useful, such as if it has been completed or if an error has occurred.
---
### Send Messages to Emails and/or Cell Phones
A MATLAB Central submission titled '[Send Messages to Emails and/or Cell Phones](http://www.mathworks.com/matlabcentral/fileexchange/48143)' by [Georgios Papachristoudis](http://www.mathworks.com/matlabcentral/fileexchange/authors/130593) accomplished the notification quite nicely. From the submission's description:
> This function sends messages to emails and/or cell phones assuming you are sending from a Gmail account. Support for text messaging only within the US. You can easily tweak the code to make it send from other accounts (other than Gmail). Please look inside the function help section for more details.
The **first thing you need do** is update Lines 66-68 in `send_msg.m` with your Gmail address and password. If you are a member of ESDL, you can use esdl.illinois@gmail.com and the default group password.
```matlab
% YOU NEED TO TYPE IN YOUR OWN EMAIL AND PASSWORDS:
mail = 'matlabsendtextmessage@gmail.com'; %Your GMail email address
pwd = 'sendtextmessagematlab'; %Your GMail password
```
You are now ready to send a test message. Be sure to include your email address and phone number in the recipients cell array.
```matlab
% your main code goes here
% notification when above code is completed
recipients = {'youremailaddress@gmail.com','XXX-XXX-XXXX'};
subject = 'Function has finished successfully';
message = 'Now go out and have fun!';
carrier = 'verizon'; % others carriers in comments
send_msg(recipients, subject, message, carrier); % send the message!
```
The function is well documented in the function comments so you should be able to find the right options there.
---
### Dynamic Notifications
We can send a message that provides additional information on the code that has just completed. Consider the following example:
```matlab
% your main code goes here
% but here is a simple optimization problem
a = 2; b = 3; c = 10;
f = @(x)bowlpeakfun(x,a,b,c);
options = optimoptions('fminunc','Algorithm','quasi-newton','display','iter');
tic
T = evalc('[x, fval] = fminunc(f,[-.5;0],options)') % no semicolon to output to command window
ttime = toc;
% notification when above code is completed
subject = [mfilename,' Function has Finished'];
lineI = 1; % reset the message line number
message{lineI} = 'Here is some more info:'; lineI = lineI + 1;
message{lineI} = ['Function: ',mfilename]; lineI = lineI + 1;
message{lineI} = ['Total computation time (s): ',num2str(ttime,5)]; lineI = lineI + 1;
message{lineI} = ['Objective function value: ',num2str(fval,5)]; lineI = lineI + 1;
message{lineI} = 'Command window output:'; lineI = lineI + 1;
message{lineI} = ''; lineI = lineI + 1;
message{lineI} = T; lineI = lineI + 1;
recipients = {'herber1@illinois.edu'};
carrier = '';
send_msg(recipients, subject, message, carrier); % send the message!
```
We first run our main code (the example uses a simple optimization problem). Then we utilize `evalc` to save the Command Window output. Next, we use the `mfilename` to automatically get the name of the function that was run. This string is the concatenated with some additional text and a dynamic subject line is created.
The message of the email can be built with a cell array containing strings. The line counter `lineI` is used with `lineI = lineI + 1`; to increment the current line number. In addition to the function name, the total computation time and final objective function value are placed in the message. Finally, the entire Command Window output is added at the end of the message (which might make for some messy emails).
Below are the email and text messages for the above code. The sent text message does not include all the lines in the above example since you are limited in the number of characters that you can send.
[](blogs/matlab/post_2/seng_msg_email.png){data-lightbox="blog_imgs" data-title=""}
[](blogs/matlab/post_2/seng_msg_text.png){data-lightbox="blog_imgs" data-title=""}
---
### Error Notifications
If there is come chance that your code will fail, you would want to be notified immediately so that you could debug the problem. We can utilize a `try/catch` statement to catch the error and send a message letting us known the event occurred. This can be used in conjuncture with the sending of a message when the code has been completed successfully. This is demonstrated in the following example where we force an error to occur:
```matlab
recipients = {'herber1@illinois.edu'};
carrier = '';
try
% your main code goes here
% but here we force an error
code = 'error(''stop'')';
tic
T = evalc(code) % no semicolon to output to command window
ttime = toc;
% notification when above code is completed
subject = [mfilename,' Function has Finished'];
lineI = 1; % reset the message line number
message{lineI} = 'Here is some more info:'; lineI = lineI + 1;
message{lineI} = ['Function: ',mfilename]; lineI = lineI + 1;
message{lineI} = ['Total computation time (s): ',num2str(ttime,5)]; lineI = lineI + 1;
message{lineI} = 'Command window output:'; lineI = lineI + 1;
message{lineI} = ''; lineI = lineI + 1;
message{lineI} = T; lineI = lineI + 1;
send_msg(recipients, subject, message, carrier); % send the message!
catch
ttime = toc; % determine how long the code was running
% notification when above code is has had an error
subject = [mfilename,' Function has Failed!'];
lineI = 1; % reset the message line number
message{lineI} = 'Failed!'; lineI = lineI + 1;
message{lineI} = 'Here is some more info:'; lineI = lineI + 1;
message{lineI} = ['Function: ',mfilename]; lineI = lineI + 1;
message{lineI} = ['Total computation time (s): ',num2str(ttime,5)]; lineI = lineI + 1;
send_msg(recipients, subject, message, carrier); % send the message!
end
```