xlswrite include date and time in filename

I can use xlswrite to write to a file.
For example write matrix Datatemp1 to sheet temp in filename.xlsx the code is
xlswrite('C:\path\filename.xlsx',Datatemp1, 'temp');
I want to include the date and time of the file in the name. I'll be collecting several files across days, and do not want one to overwrite the previous file.
thanks

 Accepted Answer

Specify your file name such:
FileName=sprintf('FileName_%s.xlsx',datestr(now));

12 Comments

Thank you for your reply, but I must be doing something wrong.
I wrote
Filename = sprintf('C:\path...\Pigeon1data_%s.xls',datestr(now));
xlswrite('Filename',Datatemp1, 'temp');
but the file is not written.
What should I write
Try to run this line alone:
Filename = sprintf('C:\path...\Pigeon1data_%s.xls',datestr(now))
The symbol '\' is causing a problem. You need to do:
Filename = sprintf('C:\\path...\\Pigeon1data_%s.xls',datestr(now))
Check the name to see if it really is what you want. It contains a whitespace.
Just use forward slashes. Believe it or not Windows recognizes them as backslash when it's in the filename.
Thanks Fangjun but still nothing is written.
Thanks Image Analyst, but again nothing is written.
if you don't get an error, something is written somewhere. Try to look for the filename in the whole C.
I get 'Filename.xls' written to the E:\ - the path specified in Matlab.
I do not get the file specified in the Filename = statement.
fname = fullfile('C:\mypath\...\', sprintf('FileName_%s.xlsx',datestr(now)))
xlswrite(fname,Datatemp1,'temp')
Substitute 'C:\mypath\...\' with your path then call xlswrite but note there are no ' around fname.
Oleg, thanks for getting me closer.
The above resulted in an error message.
The following produced the correct filename, but it has a ':' which is prohibited by windows. so the file was not written.
Filename = fullfile('C:\mypath\',sprintf('Pigeon1data %s',datestr(now)),'.xls')
the output is
C:\mypath\Pigeon1data 21-Aug-2011 17:19:32\.xls
??? Error using ==> xlswrite at 213
Invoke Error, Dispatch Exception:
Source: Microsoft Office Excel
Description: The file could not be accessed. Try one of the following:
• Make sure the specified folder exists.
• Make sure the folder that contains the file is not read-only.
• Make sure the file name does not contain any of the following characters: < > ? [ ] : | or *
• Make sure the file/path name doesn't contain more than 218 characters.
Help File: C:\Program Files\Microsoft Office\OFFICE11\1033\xlmain11.chm
Help Context ID: 0
Error in ==> xlswrite at 213
message = exceptionHandler(nargout, exception);
Error in ==> redprompt at 599
xlswrite(Filename,Datatemp1,'temp');
Filename = fullfile('C:\mypath\',sprintf('Pigeon1data_%s.xls',datestr(now,30)))
Or customize:
Filename = fullfile('C:\mypath\',sprintf('Pigeon1data_%s.xls',datestr(now,'yyyymmdd_HHMMSS')))
But .xls has to be inside the sprintf call.
Thank you Oleg!!
thank you very much for the solution Oleg

Sign in to comment.

More Answers (0)

Tags

Asked:

Jim
on 20 Aug 2011

Commented:

on 3 Aug 2015

Community Treasure Hunt

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

Start Hunting!