Saturday, September 5, 2015

Time-efficient Programming

Waiting for your code to execute is an inefficient use of time. Instead of patiently waiting by the computer, you may write a program that notifies you what happened. I've written two functions in MATLAB that help to do exactly that. The first function is called "sendToGmailFromGmail.m" which sends an email if the MATLAB script that you are testing has successfully completed. The second function is called "sendToGmailFromGmailError.m" which sends an email containing MATLAB error messages.

Let's say that A is a script, function, or an entire block of code.  Then the execution of the aforementioned functions would proceed as follows, and note the Try/Catch structure.  Also note that ME is a default MATLAB structure that contains error messages that are reported in the "Try" section of your code.

Try

A
sendToGmailFromGmail('senderEmailInQuotes@gmail.com','yourPasswordForSenderEmail','recipientEmailInQuotes@gmail.com')


Catch ME

sendToGmailFromGmailError('senderEmailInQuotes@gmail.com','yourPasswordForSenderEmail','recipientEmailInQuotes@gmail.com',ME)  

end

Using the above code, you can receive or receive automatic notifications detailing that your code has completed successfully, or an automatic notification detailing the encountered error, so that you do not have to sit next to your computer while waiting for a simulation to finish.