3. Matlab Functions


The MATLAB function text() is defined to place description texts to data points on a plot. The inclusion of the text to single data point is carried out by adding text to one point that is specified with x and y as scalars. While text to multiple points is added by specifying x and y as vectors of equal length. Customization of the display and placement of the text is supported by MATLAB such as deciding on the font size, font style, color, alignment etc. Different attributes from text() function also enable MATLAB to execute different special formats such as displaying superscripts, subscripts, and display of special characters.

Syntax of MATLAB Text

Syntax of matlab text() are given below:

text(x,y,txt)This form can be used to add a text content, specified as ‘txt’, to one or more than one data points (x,y)in the current axes.
text(x,y,z,txt)This form can be used to add a text content,specified as txt, to one or more number of,3 dimensional data points(x,y,z) in the current axes.
text(___,Name,Value)This form can be used to specify any of the Text object properties in the form of name-value pairs to edit the presentation of the text.
text(ax,___)This form can be used to create the text in the geographic, Cartesian or polar axes which is set as new axes being specified as‘ax’,instead of in the current axes i.e. gca.
t = text(___)This form can be used toreturn the text content of the plot, storing in one or more text objects. This text object can be used to modify properties of the text objects in order to change the appearance of the text, even after they are created.

Examples of MATLAB Text

Following are the examples are given below:

Example 1:

The below code snippet is written to place a single line text at one single data point.


x = 0:pi/20:2*pi;
y = sin(2*x);


The resultant plot has the configured text ‘sin(3/2π)’ at (3/2π,0) preceded by leftward arrow.

MATLAB text()-1.1

Example 2 – Configuring Same Text at Multiple Data Point

MATLAB supports the feature to configure text object at multiple data points in a plot with single command.

The below code places the text defined with the variable ‘txt’ at data points given by the vectors ‘xt’ and ‘yt’.


x = linspace(-10,10);
y = x.^2-2*x;
xt = [-4 5];
yt = [8 15];
txt = 'dy/dx = 0';


MATLAB text()-1.2

Example 3 – Placing Different Text for Different Data Point

The below code places different text objects defined by the vector ‘txt’ at different set of data points given by the vectors ‘xt’ and ‘yt’.


x = linspace(-10,10);
y = x.^2-2*x;
xt = [-4 5];
yt = [8 15];
txt = {'data point 1','data point 2'};


MATLAB text()-1.3

Example 4 – Multiline Text at Single Data Point

The code is written to place 2 text objects defined by the vector ‘str’ at one data point (12,27).


str = {'This is line 1','This is line 2'};



Example 5 – Using of Text Object to Store the Text Content

MATLAB supports editing display of the text content after the text content is being created, by having the feature to store the created text content in a text object.

The below code snippet is written to set one text content for 2 data points while creation and alter the color and font of the text content at first data point using properties of text object.

Phase 1:

x = linspace(-7,7);
y = x.^2-6*x;
t = text([-3 3],[27 -9],'A data point')



Phase 2:

t(1).Color = 'blue';
t(1).FontSize = 12;




The text object in MATLAB includes various types of attributes which contributes in customization of the appearance of the text on the plot such as:

AttributeDescriptionDefault value
FontSizeA non-zero scalar value that decides the size of the font of the displayed textDepends of OS and locale
FontWeightA string the decide the thickness of the characters in the displayed textNormal
FontNameA string that decides the font style of the displayed textFixedWidth
ColorA string that decides the color of the displayed text[0,0,0]
HorizontalAlignmentA string that decides horizontal alignment of the text withreference to the ‘position’ property x value. The probable values for this attributes are left, center or right.left
PositionIt decides the location of the text, being specified as a two-element vector of the form [x y] or a three-element vector of the form [x y z][0,0,0]
UnitsIt refers to position units, being specified as one of the values supported by
InterpreterThis attribute decides about applying markup to add subscript or superscript or special display options.Text interpreter, specified as one of these values:‘tex’ —To interpret characters using a TeX markupsubset.‘latex’ — To interpret characters using LaTeX markup subset.‘none’ — To display literal type characters.tex

Example 6

The below example refers to generating 2 plots having text content at two different data points and the customization on the appearance of the texts being applied by altering the values for the attributes Color and FontSize.


text(3,11,'A customised plot','Color','blue','FontSize',15)
hold on
text(11,21,'A customised plot','Color','green','FontSize',12)



Additional Note:

  • TeX markup is a system that is used in MATLAB to add superscripts and subscripts, modify the font style, color and also to make the text special characters supported.
  • LaTeX is a high-quality typesetting system; which includes features that are designed for the production of scientific and technical documentation. The default LaTeX font style is used by the text displayed on the plot and the impact of the properties such as FontName, FontAngleand FontWeight does not support. In order to change the font style, LaTeX markup needs to be used. The maximum character length supported byLaTeX interpreter is 1200 characters.
  • The Clipping property for text objects always remain set to ‘off’ value and the text seems to be appeared outside the axes by default. Clipping the text to the axes boundaries can be applied by setting the property value to ‘on’.

Leave a Reply

Your email address will not be published. Required fields are marked *