MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Convert Between Datetime Arrays, Numbers, and Text


In this section...
“Overview” on page 7-53
“Convert Between Datetime and Character Vectors” on page 7-54
“Convert Between Datetime and String Arrays” on page 7-55
“Convert Between Datetime and Date Vectors” on page 7-56
“Convert Serial Date Numbers to Datetime” on page 7-57
“Convert Datetime Arrays to Numeric Values” on page 7-57

Overview


datetime is the best data type for representing points in time. datetime values have
flexible display formats and up to nanosecond precision, and can account for time zones,
daylight saving time, and leap seconds. However, if you work with code authored in
MATLAB R2014a or earlier, or if you share code with others who use such a version, you
might need to work with dates and time stored in one of these three formats:


  • Date String on page 7-54 — A character vector.


Example: Thursday, August 23, 2012 9:45:44.946 AM


  • Date Vector on page 7-56 — A 1-by-6 numeric vector containing the year, month, day,
    hour, minute, and second.


Example: [2012 8 23 9 45 44.946]


  • Serial Date Number on page 7-57 — A single number equal to the number of days
    since January 0, 0000 in the proleptic ISO calendar. Serial date numbers are useful as
    inputs to some MATLAB functions that do not accept the datetime or duration data
    types.


Example: 7.3510e+005

Date strings, vectors, and numbers can be stored as arrays of values. Store multiple date
strings in a cell array of character vectors, multiple date vectors in an m-by-6 matrix, and
multiple serial date numbers in a matrix.

You can convert any of these formats to a datetime array using the datetime function.
If your existing MATLAB code expects a serial date number or date vector, use the

Convert Between Datetime Arrays, Numbers, and Text
Free download pdf