MATLAB: How to save a file with the actual date

datesave

I want to get the actual date in the following format YEAR,MONTH,DAY (YYYY_MM_DD). How can I do that? With date I get 03-Jul-2017.
filename = 'SURENAME_YYYY_MM_DD.mat';

Best Answer

If you want the current year, month, and day, try this:
tv = datestr(now, 'yyyy_mm_dd');
filename = sprintf('SURENAME_%s.mat',tv)
filename =
'SURENAME_2017_07_02.mat'
EDIT
To use my code with a datetime object, my ‘tv’ string becomes:
tv = datetime('now', 'Format','yyyy_MM_dd');
The rest of my code is unchanged.
(It’s UTC-6 here.)