how to use datestr?

10 次查看(过去 30 天)
Dear all, I tried to convert the matlab serial dates (vector) back to a date string 'yyy-MM-dd-HH-mm using the following command. I use matlab2013a. time_num is a vector of serial dates. I have attached the file please.
time_format=datestr(datetime(time_num/1440,'ConvertFrom','datenum','Format','yyyy-MM-dd HH:mm')); %saves new time in date format
Undefined function 'datetime' for input arguments of type 'double'.

采纳的回答

per isakson
per isakson 2015-5-23
编辑:per isakson 2015-5-23
With R2013a this is the way
>> vec = datevec( now )
vec =
1.0e+03 *
2.0150 0.0050 0.0230 0.0070 0.0590 0.0434
>> datestr( vec, 'yyyy-mm-dd HH:MM' )
ans =
2015-05-23 07:59
>>
I assume this still works in R2015a. However, in R2015a there are new features related to date and time. I guess, you are trying to use these new features. Which release do you use?
  3 个评论
per isakson
per isakson 2015-5-23
datetime was Introduced in R2014b
Walter Roberson
Walter Roberson 2019-4-3
nashwan sebi comments to per:
great answer, it works great thanks

请先登录,再进行评论。

更多回答(2 个)

Stephen23
Stephen23 2015-5-23
编辑:Stephen23 2015-5-23
The serial date numbers in your time_num.mat file can be converted into strings like this:
>> datestr(time_num(1:10), 'yyyy-mm-dd HH:MM')
ans =
2013-12-20 00:00
2013-12-20 00:10
2013-12-20 00:20
2013-12-20 00:30
2013-12-20 00:40
2013-12-20 00:50
2013-12-20 01:00
2013-12-20 01:10
2013-12-20 01:20
2013-12-20 01:30
Read the datestr documentation to know how this works, and also to pick the correct output format string.
  2 个评论
Teklehaimanot
Teklehaimanot 2015-5-23
Thank you Stephen,yes this is a simple way of coverting.
Teklehaimanot
Teklehaimanot 2015-5-23
I tried and it works well. Thank you again for your constant help!

请先登录,再进行评论。


Rajesh PolamReddy
I need to convert the vector Tstamp to HH:MM:SS format
datestr is converting the vector to time of string but i don't see the converted data is correct
eg : Tstamp = [4696.9,4702.9,4708.9];
datestr(x,'HH:MM:SS PM')
I got answer as below
9:36:00 PM
9:36:00 PM
9:36:00 PM
If we see the Tstamp (1) i.e 4696.9 sec is not equal to 9:36:00 PM
Can any one help me ? Iam i missing something?
  1 个评论
Stephen23
Stephen23 2018-8-7
编辑:Stephen23 2018-8-7
"If we see the Tstamp (1) i.e 4696.9 sec is not equal to 9:36:00 PM"
Actually it is a correct conversion, assuming that the input is a serial date number:
>> T = [4696.9,4702.9,4708.9];
>> datevec(T(:))
ans =
12 11 8 21 36 0
12 11 14 21 36 0
12 11 20 21 36 0
(but note that your values are not serial date numbers: this is your mistake).
"Can any one help me ?"
The MATLAB documentation can help you: "...represents each point in time as the number of days from January 0, 0000. The numeric values also can represent elapsed time in units of days"
"Iam i missing something?"
You assumed that MATLAB serial date numbers are seconds from some epoch, whereas in fact the documentation clearly describes them as being days from the 0th of January in year 0. The datestr documentation clearly describes its input as being a DateVector or DateNumber (what you used, and is measured in days).
You could either:
  • make the conversion yourself (seconds to days is trivial).
  • use a datetime object or duration object to store that data.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by