text

Writes text at a position in the axes, in data coordinates.

Category: Graphics > Formatting and Annotation

Syntax

text(x, y, txt)

Writes txt with its anchor at the point (x, y).

text(x, y, z, txt)

Places the text in 3-D axes.

text(___, Name, Value)

Sets properties such as 'FontSize', 'Color' or 'HorizontalAlignment'.

t = text(___)

Returns the text object.

Description

The text is anchored at the given point and is by default left-aligned. It is part of the axes, so it moves with the data when the limits change. Common uses are to tag individual points, label peaks and mark regions of a plot.

TeX markup, such as \alpha or x^2, is interpreted by default.

Input arguments

x, y, z
Coordinates of the anchor, in data units. Vectors place several labels at once.
txt
Text, a string array or a cell array with one entry per position.

Output arguments

t
Text object, class matlab.graphics.primitive.Text, with the properties String and Position.

Example

Label the maximum

x = 0:0.1:6;
y = sin(x);
plot(x, y)
[m, i] = max(y);
t = text(x(i), m, ' max', 'VerticalAlignment', 'bottom');
t.String
t.Position
Output
ans = ' max'
ans = 1×3

    1.6000    0.9996         0

See also

title xlabel annotation line