To complete my Matlab interface for data fitting, I needed a Timestamp function, namely a function combining date and time into a single string.
This can be useful when you want to save files having the same base "name" (e.g. foo-x11.dat, foo-x12.dat, etc) but created at different times.
In Matlab the timestamp is obtained with the command
>>datestr(clock, 0)
which output is (at the time of this post!!!)
10-nov-2012 14:10:20
Unfortunately this string cannot be used into a filename due to the presence of colons.
In
addition I would like to have a string starting with "year-month-day"
because it is a much more convenient way to name files created at
different times.
My version of TimeStamp that produces a string in the format :
year-month-day-hours-h-minutes-m-second-s
as for example
2012-11-10-14h10m20s
Here is the code! Enjoy!
And keep following this blog and my other one. It's totally worth it!
----------------------------------------------------------------
function [s]=TimeStamp
%[s]=TimeStamp
% time stamp in the format
% year-month-day-hours-h-minutes-m-second-s
% Get current time as date vector
t=clock;
% convert date vector into the string: year-month-day
yyyy_mm_dd=datestr(t, 29);
% convert date vector into the string: day-month-year[blank]hours:minutes:seconds
s=datestr(t, 0);
% get index of the blank
index=find(s==' ');
% remove the string that is before the blank
s=s((index+1):end);
% get the indices of the colons ":"
ii=find(s==':');
%change the first colon into "h" and the second one into "m". Add an "s" at the end of the
%string
h_m_s=strcat( s(1:(ii(1)-1)), 'h', s( (ii(1)+1): (ii(2)-1)), 'm', s( (ii(2)+1):end), 's');
% concate the two strings
s=strcat(yyyy_mm_dd, '-', h_m_s);
----------------------------------------------------------------
Alternatively, you can use the very short code provided by the first commenter :)
In this case:
function s=TimeStamp
s=strcat(datestr(clock,'yyyy-mm-dd-HHMM'),'m',datestr(clock,'ss'),'s');
----------------------------------------------------------------
Or the code built on the suggestion provided by the another commenter :)
function s=TimeStamp
tmp = strrep(datestr(clock), ':' , '-' );
s= strrep(tmp, ' ' , '-' );
ii=find(s=='-');
s(ii(4))='h';
s(ii(5))='m';
s=strcat(s, 's');
This does the job as well:
ReplyDeletestrcat(datestr(clock,'yyyy-mm-dd-HHhMM'),...
'm',datestr(clock,'ss'),'s')
Indeed! Thanks!
DeleteI updated the post with your solution.
There is a much simpler method:
ReplyDeletedate = strrep(datestr(clock), ':' , '-' )
strrep is a fuction that replaces certain parts of a string with another specified;
eg in the aove example : is replaced y -
There is a much simpler method:
ReplyDeletedate = strrep(datestr(clock), ':' , '-' )
strrep is a fuction that replaces certain parts of a string with another specified;
e.g in the above example : is replaced by -
You are right, I would call strrep twice to get rid of the blank between the day and the time:
ReplyDeletetmp = strrep(datestr(clock), ':' , '-' )
date = strrep(tmp, ':' , '-' )
Thanks for your contribution!
How can I display system date in a x-y plane?
ReplyDeleteI need the code for it.
Could you be more specific?
DeleteI don't get what you want to do