Converting dates into numbers
1 view (last 30 days)
Show older comments
I have a int32 matrix of dates in the format YYYYMMDD and a cell array for time in the day in the format HH:MM. How can I combine both and turn it into a number I can use for plotting?
0 Comments
Accepted Answer
Azzi Abdelmalek
on 10 Sep 2013
x=int32([19921212,19921213,19921214])'
y={'12:12', '13:13','14:14'}'
a=[num2str(x) char(y)]
b=datenum(a,'yyyymmddHH:MM')
datestr(b)
3 Comments
Silver
on 11 Sep 2018
@Azzi and when I have the date format already sticked together like this: 2016.03.01 14:38:00 in a cell array how can I convert them to numeric so I can ue them in plot ? thks in advance
More Answers (1)
Peter Perkins
on 12 Sep 2018
Unless you're using a fairly old version of MATLAB, consider using datetimes instead of datenums. You will be happier.
>> x = int32([19921212,19921213,19921214])';
>> y = {'12:12', '13:13','14:14'}';
>> d = datetime(x,'ConvertFrom','yyyymmdd')
d =
3×1 datetime array
12-Dec-1992 00:00:00
13-Dec-1992 00:00:00
14-Dec-1992 00:00:00
>> t = duration(y,'InputFormat','hh:mm')
t =
3×1 duration array
12:12:00
13:13:00
14:14:00
>> dt = d + t
dt =
3×1 datetime array
12-Dec-1992 12:12:00
13-Dec-1992 13:13:00
14-Dec-1992 14:14:00
Then just plot whatever vs. dt. You don't need to convert the datetimes to numeric.
Actually, creating the duration directly form text requires R2018a, prior to that do this:
>> t = timeofday(datetime(y,'InputFormat','HH:mm'))
t =
3×1 duration array
12:12:00
13:13:00
14:14:00
0 Comments
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!