ruler2num

Converts values shown on an axis ruler to the numbers the axes store internally.

Category: Graphics > Formatting and Annotation

Syntax

num = ruler2num(data, ruler)

Converts data, for example datetime values, to numeric positions along the ruler.

Description

Axes that show dates, durations or categories keep their positions as numbers. ruler2num gives the number that belongs to a given value, so that code can set limits, ticks or annotation positions in the units the axes use. On a date axis the numbers count days from the first day plotted.

A numeric ruler returns its input unchanged. num2ruler is the inverse.

Input arguments

data
Values of the type that the ruler displays.
ruler
An axis ruler such as ax.XAxis or ax.YAxis.

Output arguments

num
Numeric array of the same size as data.

Example

Number of the third date

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

Notes

In MatLivre the conversion is available for numeric and datetime rulers.

See also

num2ruler datetime xlim xticks gca