Main Content

datetick

Date formatted tick labels

Description

Note

It is often more convenient to plotdatetimevalues using the desired plotting function. Then you can format the tick labels using thextickformatandytickformatfunctions.datetickis useful when plotting numeric values that are serial date numbers.

datetick(tickaxis)labels the tick lines of the axis specified bytickaxisusing dates, replacing the default numeric labels.datetickselects a label format based on the minimum and maximum limits of the specified axis. The axis data values should be serial date numbers, as returned by thedatenumfunction.

example

datetick(tickaxis,dateFormat)formats the labels according todateFormat.

example

datetick(___,'keeplimits')changes the tick labels to date-based labels while preserving the axis limits. Append'keeplimits'to any of the previous syntaxes.

example

datetick(___,'keepticks')changes the tick labels to date-based labels while preserving their locations. Append'keepticks'to any of the previous syntaxes.

datetick(axes_handle,___)labels the tick lines of an axis on the axes specified byaxes_handle. Theaxes_handleargument can precede any of the input argument combinations in the previous syntaxes.

Examples

collapse all

Graph population data for the 20th Century taken from the 1990 US census and labelx-axis ticks with 2-digit years.

Create time data by decade.

t = (1900:10:1990)';

Enter total population counts for the USA.

p = [75.995 91.972 105.711 123.203 131.669...150.697 179.323 203.212 226.505 249.633]';

Convert years to serial date numbers using thedatenumfunction, and then create a bar graph of the data.

figure bar(datenum(t,1,1),p)

Figure contains an axes object. The axes object contains an object of type bar.

Replacex-axis ticks with 2-digit years. The numeric identifier11corresponds to the predefined MATLAB® date format'yy'.

dateFormat = 11; datetick('x',dateFormat)

Figure contains an axes object. The axes object contains an object of type bar.

Plot traffic count data against date ticks for hours of the day showing AM and PM.

获得流量统计数据。

loadcount.dat

Create arrays for an arbitrary date, for example, April 18, 1995.

n = length(count); year = repmat(1995,1,n); month = repmat(4,1,n); day = repmat(18,1,n);

Create arrays for each of 24 hours.

hour = 1:n; minutes = zeros(1,n);

Get the serial date numbers for the date arrays.

sdate = datenum(year,month,day,hour,minutes,minutes);

Plot a 3-D bar graph of the traffic data against the serial date numbers.

bar3(sdate,count)

Figure contains an axes object. The axes object contains 3 objects of type surface.

Label the tick lines of the graph'sy-axis with the hours of the day.

datetick('y','HHPM')

Figure contains an axes object. The axes object contains 3 objects of type surface.

Select a starting date.

startDate = datenum('02-01-1962');

Select an ending date.

endDate = datenum('11-15-2012');

Create a variable,xdata, that corresponds to the number of years between the start and end dates.

xData = linspace(startDate,endDate,50);

Plot random data.

figure stem(xData,rand(1,50))

Figure contains an axes object. The axes object contains an object of type stem.

Label thex-axis with 4-digit years, preserving thex-axis limits by using the'keeplimits'option.

datetick('x','yyyy','keeplimits')

Figure contains an axes object. The axes object contains an object of type stem.

Select a starting date.

startDate = datenum('01-01-2009');

Select an ending date.

endDate = datenum('12-31-2009');

Create a variable,xdata, that corresponds to the number of months between the start and end dates.

xData = linspace(startDate,endDate,12);

Plot random data.

figure stairs(xData,rand(1,12))

Figure contains an axes object. The axes object contains an object of type stair.

Set the number ofXTicksto the number of points inxData.

ax = gca; ax.XTick = xData;

Figure contains an axes object. The axes object contains an object of type stair.

Label thex-axis with month names, preserving the total number of ticks by using the'keepticks'option.

datetick('x','mmm','keepticks')

Figure contains an axes object. The axes object contains an object of type stair.

Input Arguments

collapse all

Axis to label with dates, specified as'x','y', or'z'.

Format of the tick line labels, specified as a character vector of symbolic identifiers or an integer that corresponds to a predefined format.

The following table shows symbolic identifiers that you can use to construct the format. You can include characters such as a hyphen, space, or colon to separate the fields. For example, to display the day of the month followed by the three-letter abbreviation of the day of the week in parentheses, usedateFormat = 'dd (ddd)'.

Note

The letter identifiers thatdatetickaccepts are different from the identifiers used by thedatetimefunction.

Symbolic Identifier

Description

Example

yyyy

Year in full

1990,2002

yy

Year in two digits

90,02

QQ

Quarter year using letterQand one digit

Q1

mmmm

Month using full name

March,December

mmm

Month using first three letters

Mar,Dec

mm

Month in two digits

03,12

m

月使用capitalized first letter

M,D

dddd

Day using full name

Monday,Tuesday

ddd

使用前三天ers

Mon,Tue

dd

Day in two digits

05,20

d

Day using capitalized first letter

M,T

HH

Hour in two digits
(no leading zeros when symbolic identifierAMorPMis used)

05,5AM

MM

分在两个数字

12,02

SS

Second in two digits

07,59

FFF

Millisecond in three digits

057

AM or PM

AMorPMinserted in text representing time

3:45:02PM

The following table lists predefined MATLAB®date formats.

Numeric Identifier

Date and Time Format

Example

-1(default)

'dd-mmm-yyyy HH:MM:SS'or'dd-mmm-yyyy'if'HH:MM:SS'= 00:00:00

01-Mar-2000 15:45:17or01-Mar-2000

0

'dd-mmm-yyyy HH:MM:SS'

01-Mar-2000 15:45:17

1

'dd-mmm-yyyy'

01-Mar-2000

2

'mm/dd/yy'

03/01/00

3

'mmm'

Mar

4

'm'

M

5

'mm'

03

6

'mm/dd'

03/01

7

'dd'

01

8

'ddd'

Wed

9

'd'

W

10

'yyyy'

2000

11

'yy'

00

12

'mmmyy'

Mar00

13

'HH:MM:SS'

15:45:17

14

'HH:MM:SS PM'

3:45:17 PM

15

'HH:MM'

15:45

16

'HH:MM PM'

3:45 PM

17

'QQ-YY'

Q1-01

18

'QQ'

Q1

19

'dd/mm'

01/03

20

'dd/mm/yy'

01/03/00

21

'mmm.dd,yyyy HH:MM:SS'

Mar.01,2000 15:45:17

22

'mmm.dd,yyyy'

Mar.01,2000

23

'mm/dd/yyyy'

03/01/2000

24

'dd/mm/yyyy'

01/03/2000

25

'yy/mm/dd'

00/03/01

26

'yyyy/mm/dd'

2000/03/01

27

'QQ-YYYY'

Q1-2001

28

'mmmyyyy'

Mar2000

29

'yyyy-mm-dd'
(ISO 8601)

2000-03-01

30

'yyyymmddTHHMMSS'
(ISO 8601)

20000301T154517

31

'yyyy-mm-dd HH:MM:SS'

2000-03-01 15:45:17

Tips

  • To change the tick spacing and locations, set the appropriate axes property (that is,XTick,YTick, orZTick) before callingdatetick.

  • Callingdateticksets theTickModeof the specified axis to'manual'. This means that after zooming, panning or otherwise changing axis limits, you should calldatetickagain to update the ticks and labels.

  • The best way to work with dates and times in MATLAB is to usedatetimevalues, which offer more features than serial date numbers. Plotdatetimevalues using theplotfunction. Use theDatetimeTickFormatname-value pair argument to modify the format of the axis tick labels.

Algorithms

datetickcalls thedatestrfunction to convert date numbers to text.

Introduced before R2006a