Append current date to filename

57 次查看(过去 30 天)
najusten
najusten 2019-3-30
编辑: najusten 2019-3-30
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 个评论
najusten
najusten 2019-3-30
编辑:najusten 2019-3-30
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 2019-3-30
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Time Series Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by