Auto attach file that I created with xlswrite?

Hi all,
I was wondering if there was a way for MATLAB to just attach a file automatically for sendmail that I made just by xlswrite instead of prompting the user with uigetfile for the attachment?
name_xls = input('What do you want to name the Excel file as?' , 's');
xlswrite(name_xls, exceloutput, '90CtT 45W','A1');
%Emails Excel sheet.
h = warndlg('Make sure your computer is connected to a secure Wifi connection! e.g: UCLA_WIFI, home network, etc.');
waitfor(h);
filepath = uigetfile('.xls','Select which Excel sheet you wish to email to Gmail.',pwd);
mail = input('Email Username (Gmail)?' , 's');
password = input('Email Password?', 's');
setpref('Internet','SMTP_Server','smtp.gmail.com');
setpref('Internet','E_mail',mail);
setpref('Internet','SMTP_Username',mail);
setpref('Internet','SMTP_Password',password);
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.auth','true');
props.setProperty('mail.smtp.socketFactory.class', 'javax.net.ssl.SSLSocketFactory');
props.setProperty('mail.smtp.socketFactory.port','465');
sendmail(mail,'LFA Analysis','Here are the results for your analysis!', filepath)

 Accepted Answer

Since the data has been saved to a file with the name of name_xls (presumably within the current directory), then rather than prompting the user for the file to send, just replace filepath with
filepath = fullfile(pwd, name_xls)
which will create a string that has the full path and name of the file to attach.

4 Comments

To clarify, yes it is in the current directory. However, I get this error code after I used your version of filepath
Error using sendmail (line 146)
Cannot open file "C:\Users\mindy\Desktop\Kamei Lab Experiments\g".
Error in exe_file_friendly_LFAanalysis1Dv1_4 (line 246)
sendmail(mail,'LFA Analysis','Here are the results for your analysis!', filepath)
Okay - I assumed that name_xls was something like 'file.xls'. Instead, it appears that you have entered just 'g' in which case, you will have to append the extension to the end of the file name
name_xls = [name_xls '.xls']
You may have to handle two cases then - the first name_xls does not have the extension (and so you will have to add it), and the second where name_xls does have the extension and so you do nothing different.
Thanks a bunch Geoff!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!