convert datetime format to numeric format

4 views (last 30 days)
Hi,
I have a matrix in datetime format
'01-Jan-1983 00:00:00'
'01-Jan-1983 01:00:00'
'01-Jan-1983 02:00:00'
'01-Jan-1983 03:00:00'
'01-Jan-1983 04:00:00'
'01-Jan-1983 05:00:00'
'01-Jan-1983 06:00:00'
'01-Jan-1983 07:00:00'
'01-Jan-1983 08:00:00'
'01-Jan-1983 09:00:00'
'01-Jan-1983 10:00:00'
'01-Jan-1983 11:00:00'
'01-Jan-1983 12:00:00'
'01-Jan-1983 13:00:00'
'01-Jan-1983 14:00:00'
'01-Jan-1983 15:00:00'
'01-Jan-1983 16:00:00'
'01-Jan-1983 17:00:00'
'01-Jan-1983 18:00:00'
'01-Jan-1983 19:00:00'
'01-Jan-1983 20:00:00'
'01-Jan-1983 21:00:00'
'01-Jan-1983 22:00:00'
'01-Jan-1983 23:00:00'
I want to convert it to numeric format as follows:
1983 1 1 0
1983 1 1 1
1983 1 1 2
1983 1 1 3
1983 1 1 4
1983 1 1 5
1983 1 1 6
1983 1 1 7
1983 1 1 8
1983 1 1 9
1983 1 1 10
1983 1 1 11
1983 1 1 12
1983 1 1 13
1983 1 1 14
1983 1 1 15
1983 1 1 16
1983 1 1 17
1983 1 1 18
1983 1 1 19
1983 1 1 20
1983 1 1 21
1983 1 1 22
1983 1 1 23
What should be the syntax for this?
  1 Comment
Jan
Jan on 28 Aug 2017
I would be useful if you post exactly, what your input is. A code to recreate it is better that the text description "I have a matrix in datetime format". You data look like a cell string. Correct?

Sign in to comment.

Accepted Answer

Jan
Jan on 28 Aug 2017
V = datevec(YourMatrix);
V = V(:, 1:4);
Depending on what exactly your input is, this might need some modifications.
  2 Comments
Ahmed Darwish
Ahmed Darwish on 23 Jul 2020
If i want to make the output time only in hours, minutes and second but in only one column, what can I do?

Sign in to comment.

More Answers (1)

Peter Perkins
Peter Perkins on 29 Aug 2017
While you asked to be able to convert timestamps strings into (partial) date vectors, you may find that if you convert your strings to datetimes, you can just work with those. You don't say what you plan on doing with the datevec, so there's no way of knowing. But one of the main reasons why the datetime data type was introduced was to replace all the conversion back and forth that was part and parcel of the old datenumn/datevec/datestr triad.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!