Main Content

datetick

(Not recommended) Date formatted tick labels

datetick is not recommended. For more information on updating your code, see Version History.

Description

datetick(tickaxis) labels the tick lines of the axis specified by tickaxis using dates, replacing the default numeric labels. datetick selects 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 the datenum function.

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

example

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

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 by axes_handle. The axes_handle argument 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 label x-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 the datenum function, and then create a bar graph of the data.

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

Bar chart with serial date numbers along the x-axis

Replace x-axis ticks with 2-digit years. The numeric identifier 11 corresponds to the predefined date format 'yy'.

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

Bar chart with two-digit years along the x-axis

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, use dateFormat = 'dd (ddd)'.

Note

The letter identifiers that datetick accepts are different from the identifiers used by the datetime function.

Symbolic Identifier

Description

Example

yyyy

Year in full

1990, 2002

yy

Year in two digits

90, 02

QQ

Quarter year using letter Q and 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

Month using capitalized first letter

M, D

dddd

Day using full name

Monday, Tuesday

ddd

Day using first three letters

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 identifier AM or PM is used)

05, 5 AM

MM

Minute in two digits

12, 02

SS

Second in two digits

07, 59

FFF

Millisecond in three digits

057

AM or PM

AM or PM inserted in text representing time

3:45:02 PM

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:17 or 01-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, or ZTick) before calling datetick.

  • Calling datetick sets the TickMode of the specified axis to 'manual'. This means that after zooming, panning or otherwise changing axis limits, you should call datetick again to update the ticks and labels.

  • The best way to work with dates and times in MATLAB is to use datetime values, which offer more features than serial date numbers. Plot datetime values using the plot function. Use the DatetimeTickFormat name-value pair argument to modify the format of the axis tick labels.

Algorithms

datetick calls the datestr function to convert date numbers to text.

Version History

Introduced before R2006a

collapse all

R2024a: Not recommended

There are no plans to remove datetick. However, you can use any plotting function to plot datetime or duration values instead. Plotting datetime and duration values has these advantages:

  • You can configure the tick labels using the xtickformat and ytickformat functions.

  • You can customize aspects of your plot by setting properties of the DatetimeRuler object. For example, you can display datetime values in a variety of formats, include a time zone, or display a time zone offset.