| MATLAB® | ![]() |
datetick(tickaxis)
datetick(tickaxis,dateformat)
datetick(tickaxis,dateformnum)
datetick(...,'keeplimits')
datetick(...,'keepticks')
datetick(axes_handle,...)
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 Format | Interpretation of Format Symbol |
|---|---|
| yyyy | Full year, e.g., 1990, 2001, or 2008 |
| yy | Partial year, e.g. 90, 01, or 08 |
| mmmm | Full name of the month, according to the calendar locale, e.g., "March" or "April" in the UK and USA English locales |
| mmm | First three letters of the month, according to the calendar locale, e.g., "Mar" or "Apr" in the UK and USA English locales |
| mm | Numeric month of year, padded with leading zeros, e.g., ../03/.. or ../12/.. |
| m | Capitalized first letter of the month, according to the calendar locale; for backwards compatibility, e.g., "D" for December |
| dddd | Full name of the weekday, according to the calendar locale, e.g., "Monday" or "Tuesday", for the UK and USA calendar locales |
| ddd | First three letters of the weekday, according to the calendar locale, e.g., "Mon" or "Tue", for the UK and USA calendar locales |
| dd | Numeric 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 |
| HH | Hour 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. |
| MM | Minutes of the hour, padded with leading zeros, e.g., 10:05 or 10:05 AM |
| SS | Second of the minute, padded with leading zeros, e.g., 10:15:30, 10:05:30, 10:05:30 AM |
| FFF | Milliseconds field, padded with leading zeros, e.g., 10:15:30.015 |
| PM | Setting 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 |
|---|---|---|
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.
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.
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)

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')

The axes properties: XTick, YTick, and ZTick
Annotating Plots for related functions
![]() | datestr | datevec | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |