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

 Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 20 de Ag. de 2011
Specify your file name such:
FileName=sprintf('FileName_%s.xlsx',datestr(now));

12 comentarios

Jim
Jim el 20 de Ag. de 2011
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
Fangjun Jiang
Fangjun Jiang el 21 de Ag. de 2011
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.
Image Analyst
Image Analyst el 21 de Ag. de 2011
Just use forward slashes. Believe it or not Windows recognizes them as backslash when it's in the filename.
Jim
Jim el 21 de Ag. de 2011
Thanks Fangjun but still nothing is written.
Thanks Image Analyst, but again nothing is written.
Oleg Komarov
Oleg Komarov el 21 de Ag. de 2011
if you don't get an error, something is written somewhere. Try to look for the filename in the whole C.
Jim
Jim el 21 de Ag. de 2011
I get 'Filename.xls' written to the E:\ - the path specified in Matlab.
I do not get the file specified in the Filename = statement.
Oleg Komarov
Oleg Komarov el 21 de Ag. de 2011
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.
Jim
Jim el 21 de Ag. de 2011
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');
Oleg Komarov
Oleg Komarov el 21 de Ag. de 2011
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.
Jim
Jim el 21 de Ag. de 2011
Thank you Oleg!!
Oleg Komarov
Oleg Komarov el 21 de Ag. de 2011
Happy to help.
aryan PATEL
aryan PATEL el 3 de Ag. de 2015
thank you very much for the solution Oleg

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

Jim
el 20 de Ag. de 2011

Comentada:

el 3 de Ag. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by