Categories

# MATLAB Text

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:

## 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.

## Code:

x = 0:pi/20:2*pi;
y = sin(2*x);
str=strcat("\leftarrow","sin(","3/2","\pi)");
plot(x,y)
text(3/2*pi,0,str)

## Output:

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

## 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’.

## Code:

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

## 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’.

## Code:

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

## 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).

## Code:

plot(5:50)
str = {'This is line 1','This is line 2'};
text(12,27,str)

## 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;
plot(x,y)
t = text([-3 3],[27 -9],'A data point')

## Phase 2:

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

## Output:

### Attributes

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:

## 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.

## C0de:

subplot(1,2,1)
plot(1:15)
text(3,11,'A customised plot','Color','blue','FontSize',15)
subplot(1,2,2)
hold on
plot(1:25)
text(11,21,'A customised plot','Color','green','FontSize',12)