Append current date to filename

71 views (last 30 days)
najusten
najusten on 30 Mar 2019
Edited: najusten on 30 Mar 2019
Hello everyone,
I have a simulink code which writes a structure called 'state' to the workspace containing multiple outputs which I would like to save to a .mat file by running a post processing script, eventually through a callback function. I am currently trying to append the current time and date to the output file. I have gotten the file to save in the specified location but have been unable to append the current date/time to the file name. I have included the path because I would like to save the output files to their own folder, not simply the current folder.
This was my latest attempt..
Any help would be appreciated.
Cheers!
t = datetime('now', 'Format','yyyy_MM_dd,HH_mm');
DateString = datestr(t)
FileName=[DateString,' Output ','JohnSmith']
save('/Users/John/Desktop/Project1/Matlab Model/'FileName,'state')
  2 Comments
najusten
najusten on 30 Mar 2019
Edited: najusten on 30 Mar 2019
UPDATE: I have gotten the file to save with the current date using the following code:
t = datetime('now','Format','yyyy-MM-dd''_T''HHmmss')
t2 = datevec(t)
DateString = datestr(t2)
FileName=[DateString,' Output ','JohnSmith']
save( fullfile('/Users/John/Desktop/Project1/Matlab Model/', FileName) )
My issue now is that I want the files to save in the format specified using datetime, namely 'yyyy-MM-dd', however whenever I convert the files using DataString, Matlab converts them to the format:
'30-Mar-2019 13:29:35 Output JohnSmith'
This messes up the file ordering. What is the best way to prevent this? I could not get the 'datenum' or 'datevec' commands to work as suggested by some of the threads I was reading.
Thanks!
najusten
najusten on 30 Mar 2019
Well would you look at that..
S = char(t);
FileName=[S,' Output ','JohnSmith']
Seems to solve my problems. Legitimately tried to get this to work for 2 hours last night.. In my defense it was late.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!