how to use datestr?

5 vues (au cours des 30 derniers jours)
Teklehaimanot
Teklehaimanot le 23 Mai 2015
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'.

Réponse acceptée

per isakson
per isakson le 23 Mai 2015
Modifié(e) : per isakson le 23 Mai 2015
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 commentaires
per isakson
per isakson le 23 Mai 2015
datetime was Introduced in R2014b
Walter Roberson
Walter Roberson le 3 Avr 2019
nashwan sebi comments to per:
great answer, it works great thanks

Connectez-vous pour commenter.

Plus de réponses (2)

Stephen23
Stephen23 le 23 Mai 2015
Modifié(e) : Stephen23 le 23 Mai 2015
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 commentaires
Teklehaimanot
Teklehaimanot le 23 Mai 2015
Thank you Stephen,yes this is a simple way of coverting.
Teklehaimanot
Teklehaimanot le 23 Mai 2015
I tried and it works well. Thank you again for your constant help!

Connectez-vous pour commenter.


Rajesh PolamReddy
Rajesh PolamReddy le 7 Août 2018
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 commentaire
Stephen23
Stephen23 le 7 Août 2018
Modifié(e) : Stephen23 le 7 Août 2018
"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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by