Syntax
Description
str = date
returns a string containing the date in dd-mmm-yyyy
format.
MATLAB Function Reference |
Syntax
Description
The datenum
function converts date strings and date vectors (defined by datevec
) into serial date numbers. Date numbers are serial days elapsed from some reference date. By default, the serial day 1 corresponds to 1-Jan-0000.
N = datenum(DT)
converts the date string or date vector DT
into a serial date number. Date strings with two-character years, e.g., 12-june-12
, are assumed to lie within the 100-year period centered about the current year.
Note If DT is a string, it must be in one of the date formats 0 , 1 , 2 , 6 , 13 , 14 , 15 , 16, or 23 as defined by datestr . |
N = datenum(DT,P)
uses the specified pivot year as the starting year of the 100-year range in which a two-character year resides. The default pivot year is the current year minus 50 years.
N = datenum(Y,M,D)
returns the serial date number for corresponding elements of the Y
, M
, and D
(year, month, day) arrays. Y
, M
, and D
must be arrays of the same size (or any can be a scalar). Values outside the normal range of each array are automatically "carried" to the next unit.
N = datenum(Y,M,D,H,MI,S)
returns the serial date number for corresponding elements of the Y
, M
, D
, H
, MI
, and S
(year, month, day, hour, minute, and second) array values. Y
, M
, D
, H
, MI
, and S
must be arrays of the same size (or any can be a scalar). Values outside the normal range of each array are automatically carried to the next unit (for example month values greater than 12 are carried to years). Month values less than 1 are set to be 1. All other units can wrap and have valid negative values.
Examples
Convert a date string to a serial date number.
Specifying year, month, and day, convert a date to a serial date number.
Convert a date vector to a serial date number.
Convert a date string to a serial date number using the default pivot year
Convert the same date string to a serial date number using 1900 as the pivot year.
datestr
Syntax
Description
The datestr
function converts serial date numbers (defined by datenum
) and date vectors (defined by datevec
) into date strings.
str = datestr(DT,dateform)
converts a single date vector, or each element of an array of serial date numbers to a date string. Date strings with two-character years, e.g., 12-june-12
, are assumed to lie within the 100-year period centered about the current year.
str = datestr(DT,dateform,P)
uses the specified pivot year as the starting year of the 100-year range in which a two-character year resides. The default pivot year is the current year minus 50 years.
The optional argument dateform
specifies the date format of the result. dateform
can be either a number or a string:
NOTE dateform numbers 0 , 1 , 2 , 6 , 13 , 14 , 15 , 16 , and 23 produce a string suitable for input to datenum or datevec . Other date string formats will not work with these functions. |
'h:m:s'
, 'h:m:s.s'
, 'h:m pm'
, ... can also be part of the input array DT
. If you do not specify dateform
, or if you specify dateform
as -1
, the date string format defaults todatetick
Syntax
Description
datetick(tickaxis)
labels the tick lines of an axis using dates, replacing the default numeric labels. tickaxis
is the string 'x'
, 'y'
, or 'z'
. The default is 'x'
. datetick
selects a label format based on the minimum and maximum limits of the specified axis.
datetick(tickaxis,
formats the labels according to the integer dateform
) dateform
(see table). To produce correct results, the data for the specified axis must be serial date numbers (as produced by datenum
).
Remarks
datetick
calls datestr
to convert date numbers to date strings.
To change the tick spacing and locations, set the appropriate axes property (i.e., XTick
, YTick
, or ZTick
) before calling datetick
.
Example
Consider graphing population data based on the 1990 U.S. census:
t = (1900:10:1990)'; %
Time interval
p = [75.995 91.972 105.711 123.203 131.669 ...
150.697 179.323 203.212 226.505 249.633]'; %Population
plot(datenum(t,1,1),p) %Convert years to date numbers and plot
grid on
datetick('x',11) % Replace x-axis ticks with 2-digit year labels
datevec
Description
C = datevec(A)
splits its input into an n
-by-6 array with each row containing the vector [Y,M,D,H,MI,S]
. The first five date vector elements are integers. Input A
can either consist of strings of the sort produced by the datestr
function, or scalars of the sort produced by the datenum
and now
functions. Date strings with two-character years, e.g., 12-june-12
, are assumed to lie within the 100-year period centered about the current year.
C = datevec(A,P)
uses the specified pivot year as the starting year of the 100-year range in which a two-character year resides. The default pivot year is the current year minus 50 years.
[Y,M,D,H,MI,S] = datevec(A)
returns the components of the date vector as individual variables.
When creating your own date vector, you need not make the components integers. Any components that lie outside their conventional ranges affect the next higher component (so that, for instance, the anomalous June 31 becomes July 1). A zeroth month, with zero days, is allowed.
Examples
An example of using a string as input:
An example of using a serial date number as input:
No comments:
Post a Comment