Quantcast

Documentation Center

  • Trial Software
  • Product Updates

datetick

Date formatted tick labels

Syntax

  • datetick(tickaxis)
  • datetick(tickaxis,dateFormat) example

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.

example

datetick(tickaxis,dateFormat) formats the labels according to the string 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.

example

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

example

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

expand all

Label x-Axis Ticks with 2-digit Years

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 plot the data.

figure
plot(datenum(t,1,1),p)
grid on

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

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

Label x-Axis Ticks with Hours of the Day

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

Get traffic count data.

load count.dat

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

n = length(count);
year = 1990 * ones(1,n);
month = 4 * ones(1,n);
day = 18 * ones(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.

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

Plot the traffic data against the serial date numbers.

figure
plot(xdate,count)

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

datetick('x','HHPM')

Label x-Axis and Preserve Axis Limits

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
plot(xData,rand(1,50))

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

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

Add Month Labels to Plot and Preserve Number of Ticks

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
plot(xData,rand(1,12))

Set the number of XTicks to the number of points in xData.

set(gca,'XTick',xData)

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

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

Create Multiple Plots Within Figure and Label Axis with Month Names

Select a starting date and an ending date.

startDate = datenum('01-01-2009');
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
s(1)=subplot(2,1,1);
plot(xData,rand(1,12))
s(2) = subplot(2,1,2);
plot(xData,rand(1,12))

Set the number of XTicks to the number of points in xData. Label the x-axis of each subplot with month names, referring to each subplot using its axes handle. Preserve the total number of ticks by using the 'keepticks' option.

set(s,'XTick',xData)
for i = 1:2
	datetick(s(i),'x','mmm','keepticks')
end

Input Arguments

expand all

tickaxis — Axis to label'x' (default) | 'y' | 'z'

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

dateFormat — Format of tick line labelsstring | integer

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

The following table shows symbolic identifiers you can use to construct the dateFormat string. 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)'.

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 date string

3:45:02 PM

The following table lists predefined MATLAB® date formats.

Numeric Identifier

Date String 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

More About

expand all

Tips

  • To change the tick spacing and locations, set the appropriate axes property (i.e., 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.

Algorithms

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

See Also

| | | |

Was this topic helpful?