# MATLAB Draw Arrow

Ammar Ali May 21, 2021 May 09, 2021

This tutorial will discuss adding an arrow on a plot using the `annotation()` and `text()` function in MATLAB.

## Add an Arrow on a Plot Using the `annotation()` Function in MATLAB

To add an arrow with some text on a plot in MATLAB, you can use the `annotation()` function. The `annotation()` function creates an arrow with given dimensions on the plot. For example, let’s plot a sine wave and add an arrow to it. See the code below.

``````t = 1:0.01:2;
x = sin(2*pi*t);
figure
plot(t,x)
dimen = [.3 .1 .5 .5];
a = annotation('arrow','Position',dimen)
``````

Output:

In the above code, you can change the dimensions of the arrow according to your requirements. You can also change the color of the arrow using the `Color` property, the line style using the `LineStyle` property, the line width using the `LineWidth` property, the style of the head using the `HeadStyle` property, and position of the arrow using the `Position` property. For example, let’s change all these properties of the arrow using a function handle. See the code below.

``````t = 1:0.01:2;
x = sin(2*pi*t);
figure
plot(t,x)
dimen = [.3 .1 .5 .5];
a = annotation('arrow',[0.31 0.4],[0.7 0.8])
a.Position = dimen;
a.Color = [1 0 0];
a.LineWidth = 2;
``````

Output:

In the above code, we changed the color of the arrow to red, the line width of the arrow to 2, and the style of the head to `vback3`. The function handle `a` will display all the arrow properties on the command window, which we can change using the function handle `a`. Check this link for more details about the `annotation()` function.

## Adding an Arrow on a Plot Using the `text()` Function in MATLAB

You can also use the `text()` function to add an arrow to the plot. You need to pass the x and y coordinate on which you want to place the text along with the arrow. Simply plot the variable, select the coordinates from the plot, and then use the `text()` function to place the text and the arrow on the selected coordinates. If you give the coordinates which don’t lie on the plot, you won’t see the text and the arrow. You can also change the properties of the text like the font size using the `FontSize` property and the color using the `Color` property etc. For example, let’s plot a cosine wave and put some text with an arrow on it with font size 18 and red color and a black box around the text. See the code below.

``````t = 1:0.01:2;
plot(cos(2*pi*t))
tex = text(20,0.8,'\leftarrowCosine wave','FontSize',18,'Color','r')
``````

Output:

You can give your desired color to the text using the `Color` property and the font size using the `FontSize` property. You can also add multiple texts with arrows at multiple positions on the plot with different text, line styles, color, size, and box. Check this link for more details about the `text()` function.

Author: Ammar Ali

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.