MATLAB: Saving figure

figuresave

Hi,
I have an m file which generates a figure. I run this m file daily. But each day, I have to name and manually save the figure. Is there a way to automatically save the figure with todays date as the file name? I know how to automatically save the figure but I am not sure how to use todays date as the file name. Thanks.

Best Answer

Use datestr() but be sure to change the colons to a valid character!!!
Try this:
% Get date-time string.
baseFileName = datestr(now)
% Colons aren't allowed in filenames.
% Replace them with underlines
baseFileName = strrep(baseFileName, ':', '_');
% Add extension.
baseFileName = [baseFileName '.png']
Then call export_fig() as usual.