num2ruler

Converts numeric values to the data type that an axis ruler uses.

Category: Graphics > Formatting and Annotation

Syntax

data = num2ruler(num, ruler)

Converts the numbers num to values of the type shown on the ruler, such as datetime on a time axis.

Description

Axes with datetime, duration or categorical data store positions as plain numbers internally. num2ruler translates such a position back to the value a reader sees on the axis, and ruler2num goes the other way.

This is useful when a property such as a limit or a tick is stored numerically but the code needs the corresponding date.

Input arguments

num
Numeric array of positions along the axis.
ruler
An axis ruler, for example ax.XAxis or ax.YAxis.

Output arguments

data
Values in the ruler's own type. For a numeric ruler they are the numbers unchanged.

Example

Position on a date axis

t = datetime(2024,1,1) + days(0:4);
plot(t, [1 4 2 5 3])
ax = gca;
n = ruler2num(t(3), ax.XAxis)
d = num2ruler(n, ax.XAxis)
Output
n = 2
d = datetime
   03-Jan-2024

Notes

In MatLivre the conversion is available for numeric and datetime rulers. Other ruler types are not converted.

See also

ruler2num datetime xlim xticks gca