Be the first to rate this file! 3 Downloads (last 30 days) File Size: 376 Bytes File ID: #22502

Text message / E-mail Matlab!

by Stefan Novak

 

20 Dec 2008

Send a text message or e-mail to a Gmail account and have Matlab evaluate it and return the results.

| Watch this File

File Information
Description

Have you ever had a need to invert a matrix while out for dinner? Have you ever had the need to solve a second-order differential equation while stuck in traffic but didn't have access to Matlab? Well, now you can!

This is a VBA module file (*.bas) that allows you to evaluate a string in Matlab by calling the Matlab interface. In order to get it fully functional, you have to integrate it with Microsoft Outlook by using the following steps:

Step 1) Make a Gmail account of your choice.
Step 2) Add the Gmail account to Microsoft Outlook via Gmail's IMAP interface.
Step 3) Open the Visual Basic Editor. (Alt-F11)
Step 4) Add the Matlab Server Automation... Reference. (Tools > References)
Step 5) Double click the ThisOutlookSession module in the Project window.
Step 6) In the left pull-down menu for the module window, select Application. In the right pull-down menu, select NewMailEx.
Step 7) You have to use a function that (safely) bypasses some of VBA's security dialogs. Use the following code for NewMailEx:

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
    Dim iMsg, iConf, Flds
    Dim varEntryIDs
    Dim objItem
    Dim i As Integer
    Dim matlabStr As String
    
    Dim strID As String
    Dim olNS As Outlook.NameSpace
    Dim olMail As Outlook.MailItem
    
    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")
    Set Flds = iConf.Fields

    schema = "http://schemas.microsoft.com/cdo/configuration/"
    Flds.Item(schema & "sendusing") = 2
    Flds.Item(schema & "smtpserver") = "smtp.gmail.com"
    Flds.Item(schema & "smtpserverport") = 465
    Flds.Item(schema & "smtpauthenticate") = 1
    Flds.Item(schema & "sendusername") = "your.gmail.user.name@gmail.com"
    Flds.Item(schema & "sendpassword") = "your.gmail.password"
    Flds.Item(schema & "smtpusessl") = 1
    Flds.Update
    
    varEntryIDs = Split(EntryIDCollection, ",")
    
    For i = 0 To UBound(varEntryIDs)
        
        strID = varEntryIDs(i)
        Set olNS = Application.GetNamespace("MAPI")
        Set olMail = olNS.GetItemFromID(strID)
        
        matlabStr = EvalStrInML(olMail.Body)
        matlabStr = Replace(matlabStr, Chr(10), "<br>")
        
        With iMsg
        .To = olMail.SenderEmailAddress
        .From = "Your Name <your.gmail.user.name@gmail.com>"
        .Subject = "Matlab Results"
        .HTMLBody = "<br>" & ">> " & olMail.Body & "<br>" & matlabStr
        .Sender = "Your Name"
        .Organization = ""
        .ReplyTo = "your.gmail.user.name@gmail.com"
        Set .Configuration = iConf
        SendEmailGmail = .Send
        End With
        
        Set iMsg = Nothing
        Set iConf = Nothing
        Set Flds = Nothing
        
    Next
    
    Set olMail = Nothing
    Set olNS = Nothing
End Sub

Step 8) Right-click the Modules folder on the Project panel and click Import File. Select the *.bas file EvalStrInML.bas file.

Step 9) Change your Send/Receive settings to check for e-mail every 1 minute.

You should be good to go! Any e-mails that gets sent to your.gmail.user.name@gmail.com will show up in your Outlook account. When they do, Outlook will parse the body of the e-mail and send it over to Matlab for evaluation. Outlook then takes the results and sends an e-mail back out through your Gmail account.

Here's an example:

From: Stefan Novak <*********@gmail.com>
To: My Matlab Email Interface <*********@gmail.com>
Subject: Let's try a test!
Body: inv([5,8,2;4,10,2;9,3,7])

The reply:

From: My Matlab Email Interface <********@gmail.com>
To: Stefan Novak <*********@gmail.com>
Subject: Matlab Results
Body:
>> inv([5,8,2;4,10,2;9,3,7])

ans =

0.7619 -0.5952 -0.0476
-0.1190 0.2024 -0.0238
-0.9286 0.6786 0.2143

Note: This also works with text messages (as long as your phone is capable of text messaging e-mail address.)

MATLAB release MATLAB 7.7 (R2008b)
Other requirements Microsoft Outlook 2003 or 2007
Tags for This File  
Everyone's Tags
communications(2), email, gmail, matlab, outlook, text message, vba, visual basic for applications
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (3)
18 Jul 2012 James Campbell

is it possible to modify the code so that an email sent to my outlook inbox could do the same thing. My company block the use of gmail accounts so I can't get this to work. Thanks, James

19 Sep 2010 lin

I'm trying to do this for many times. But I fail. Thanks for your Great Programe!

25 Feb 2009 AHMET ANIL DINDAR

It seems very interesting, I'll try immediately and give my opinions.

Contact us