datetick - Date formatted tick labels

Syntax

datetick(tickaxis)
datetick(tickaxis,dateformat)
datetick(tickaxis,dateformnum)
datetick(...,'keeplimits')
datetick(...,'keepticks')
datetick(axes_handle,...)

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. The axis data values should be generated by or be compatible with the output of the datenum function.

datetick(tickaxis,dateformat) formats the labels according to the string dateformat. A date format string can consist of the following elements (or combinations of them), identified by the format symbols in the left-hand column.

Date FormatInterpretation of Format Symbol
yyyyFull year, e.g., 1990, 2001, or 2008
yyPartial year, e.g. 90, 01, or 08
mmmmFull name of the month, according to the calendar locale, e.g., "March" or "April" in the UK and USA English locales
mmmFirst three letters of the month, according to the calendar locale, e.g., "Mar" or "Apr" in the UK and USA English locales
mmNumeric month of year, padded with leading zeros, e.g., ../03/.. or ../12/..
mCapitalized first letter of the month, according to the calendar locale; for backwards compatibility, e.g., "D" for December
ddddFull name of the weekday, according to the calendar locale, e.g., "Monday" or "Tuesday", for the UK and USA calendar locales
dddFirst three letters of the weekday, according to the calendar locale, e.g., "Mon" or "Tue", for the UK and USA calendar locales
ddNumeric day of the month, padded with leading zeros, e.g., 05/../.. or 20/../..
d Capitalized first letter of the weekday, e.g., "M" for Monday; for backwards compatibility
HHHour of the day, according to the time format. In case the time format AM | PM is set, HH does not pad with leading zeros. If AM | PM is not set, HH displays the hour of the day, padded with leading zeros; e.g., 10:20 PM, which is equivalent to 22:20; 9:00 AM, which is equivalent to 09:00.
MMMinutes of the hour, padded with leading zeros, e.g., 10:05 or 10:05 AM
SSSecond of the minute, padded with leading zeros, e.g., 10:15:30, 10:05:30, 10:05:30 AM
FFFMilliseconds field, padded with leading zeros, e.g., 10:15:30.015
PMSetting the time format to morning or afternoon by appending AM or PM to the date string, as appropriate, without separating symbols

You can mix format symbols to create customized data symbols. For example:

 datetick('x','dd (ddd)')

generates ticks along the x-axis that display the day of the month followed by the three-letter abbreviation of the day of the week in parentheses, for example, 01 (Wed). To preface each date tick with an abbreviated month name, you could specify

datetick('x','mmm-dd (ddd)')

to yield ticks such as Apr-01 (Wed).

datetick(tickaxis,dateformnum) formats the labels according to the integer dateformnum, a date format index (see table). To produce correct results, the data for the specified axis must be serial date numbers (as produced by datenum).

Date Format Number

dateformat (string)

Example

0

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

01-Mar-2008 15:45:17

1

'dd-mmm-yyyy'

01-Mar-2008

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

(ISO 8601) 'yyyy-mm-dd'

2000-03-01

30

(ISO 8601) 'yyyymmddTHHMMSS'

20000301T154517

31

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

2000-03-01 15:45:17

datetick(...,'keeplimits') changes the tick labels to date-based labels while preserving the axis limits.

datetick(...,'keepticks') changes the tick labels to date-based labels without changing their locations.

You can use both keeplimits and keepticks in the same call to datetick.

datetick(axes_handle,...) uses the axes specified by the handle ax instead of the current axes.

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.

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.

Examples

Example 1 — Plot US Population Across the 20th Century

Graph population data for the 20th Century taken from the 1990 US census.

% 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]';n
% Convert years to date numbers and plot
plot(datenum(t,1,1),p)	
grid on
% Replace x-axis ticks with 2-digit years using date format 11
datetick('x',11)	

Example 2 — Plot Hourly Traffic Counts by AM and PM

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, here 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;
min = zeros(1,n);
% Get the datenums for the data (only hours change)
xdate = datenum(year,month,day,hour,min,min);
% Plot the traffic data against datenums
plot(xdate,count)
% Update the graph's x-axis with date ticks
datetick('x','HHPM')

See Also

The axes properties: XTick, YTick, and ZTick

datenum, datestr

Annotating Plots for related functions

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS