MATLAB: Export CSV file with timestamp in filename

csvexportexport datatimestamp

I want to export some data using writetable into a .csv file but I also want a timestamp in the name of the .csv file. I have been able to export the data to a .csv file and I have been able to export the data with the timestamp but ONLY as a .txt file. Any advice??

Best Answer

I’m not certain exactly what you want, but this will get you started:
filename = sprintf('CSV File %s.csv', datestr(now));
produces:
filename =
CSV File 02-Feb-2015 10:16:58.csv
You can specify the format of the timestamp with the ‘formatOut’ parameter of datestr, for example:
filename = sprintf('CSV File %s.csv', datestr(now, 'dd.mm.yyyy HH:MM'))
produces:
filename =
CSV File 02.02.2015 10:20.csv