MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Converting Date Vector Returns Unexpected Output


Because a date vector is a 1-by-6 vector of numbers, datestr might interpret your input
date vectors as vectors of serial date numbers, or vice versa, and return unexpected
output.

Consider a date vector that includes the year 3000. This year is outside the range of years
that datestr interprets as elements of date vectors. Therefore, the input is interpreted
as a 1-by-6 vector of serial date numbers:

datestr([3000 11 05 10 32 56])

ans =

18-Mar-0008
11-Jan-0000
05-Jan-0000
10-Jan-0000
01-Feb-0000
25-Feb-0000

Here datestr interprets 3000 as a serial date number, and converts it to the date string
'18-Mar-0008'. Also, datestr converts the next five elements to date strings.

When converting such a date vector to a character vector, first convert it to a serial date
number using datenum. Then, convert the date number to a character vector using
datestr:

dn = datenum([3000 11 05 10 32 56]);
ds = datestr(dn)

ds =

05-Nov-3000 10:32:56

When converting dates to character vectors, datestr interprets input as either date
vectors or serial date numbers using a heuristic rule. Consider an m-by-6 matrix. datestr
interprets the matrix as m date vectors when:


  • The first five columns contain integers.

  • The absolute value of the sum of each row is in the range 1500–2500.


7 Dates and Time

Free download pdf