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일

1 개 추천

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

Teklehaimanot
Teklehaimanot 2015년 5월 23일
Thank you Per Isakson. yes it works in 2013a. I am using matlab 2013a.
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일

1 개 추천

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
Rajesh PolamReddy 2018년 8월 7일

0 개 추천

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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

태그

질문:

2015년 5월 23일

댓글:

2019년 4월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by