Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Matlab Annotation

Introduction to Matlab Annotation

We have 2 types of annotations in Matlab:

  • Line Type
  • Shape Type

Syntax:

annotation (lineType, A, B)

annotation (shapeType, dim)

Description:

  • annotation (lineType, a, b): It is used to create an arrow or a line annotation. This annotation is extended between the 2 points in the figure.
  • annotation (shapeType, dim): It is used to create a shape annotation of defined size and location.

The ‘lineType’ argument can take following 4 values:

  • Line
  • Arrow
  • Double Arrow
  • Text Arrow

The ‘shapeType’ argument can take following values:

  • Rectangle
  • Textbox

Examples of Matlab Annotation

Given below are the examples mentioned:

Example #1

In this example, we will plot a Sine Wave and then will use line annotation to show the first incident when this sine wave touches the maximum value.

We will follow the following steps:

  • Create the sine plot.
  • Initialize the points for annotation line.
  • Pass these points as arguments to the annotation function.

Code:

Fs = 0:pi/50:2*pi;

[Defining the frequency for sinewave]

y = sin(Fs);

[Initializing the sine wave]

plot(Fs,y)

[Creating the plot of sine wave]

A = [0.3 0.3];

B = [0.8 0.9];

[Defining the points for the annotation]

annotation(‘line’, A, B)

[Passing the above points to annotation function]

Input:

Fs = 0:pi/50:2*pi;
y = sin(Fs);
plot(Fs,y)
A = [0.3 0.3];
B = [0.8 0.9];
annotation('line', A, B)

Output:

As we can see in the output, the first peak of the sine wave is being pointed out using annotation line.

Example #2

In this example, we will use the arrow annotation to show the first incident when our sine wave touches the maximum value.

Code:

Fs = 0:pi/50:2*pi;

[Defining the frequency for sinewave]

y = sin(Fs);

[Initializing the sine wave]

plot(Fs,y)

[Creating the plot of sine wave]

A = [0.3 0.3];

B = [0.8 0.9];

[Defining the points for the annotation]

annotation(‘arrow’, A, B)

[Passing the above points to annotation function. Please note that first argument in this case is ‘arrow’]

Input:

Fs = 0:pi/50:2*pi;
y = sin(Fs);
plot(Fs,y)
A = [0.3 0.3];
B = [0.8 0.9];
annotation('arrow', A, B)

Output:

As we can see in the output, the first peak of the sine wave is being pointed out using annotation arrow.

Example #3

In this example, we will use the double arrow annotation to show the first incident when our sine wave touches the maximum value.

Code:

Fs = 0:pi/50:2*pi;

[Defining the frequency for sinewave]

y = sin(Fs);

[Initializing the sine wave]

plot(Fs,y)

[Creating the plot of sine wave]

A = [0.3 0.3];

B = [0.8 0.9];

[Defining the points for the annotation]

annotation(‘doublearrow’, A, B)

[Passing the above points to annotation function. Please note that the first argument in this case is ‘doublearrow’]

Input:

Fs = 0:pi/50:2*pi;
y = sin(Fs);
plot(Fs,y)
A = [0.3 0.3];
B = [0.8 0.9];
annotation('doublearrow', A, B)

Output:

As we can see in the output, the first peak of the sine wave is being pointed out using annotation double arrow.

All the above annotation types help us to put a line or arrow, but what if we need text also along with the annotation line. For this purpose, we use ‘textarrow’ annotation.

Example #4

In this example, we will use the text arrow annotation to show the first incident when our sine wave touches the maximum value.

Code:

Fs = 0:pi/50:2*pi;

[Defining the frequency for sinewave]

y = sin(Fs);

[Initializing the sine wave]

plot(Fs,y)

[Creating the plot of sine wave]

A = [0.3 0.3];

B = [0.8 0.9];

[Defining the points for the annotation]

messageToDisplay = ‘First Maxima’

[Initializing the string with text message]

annotation(‘textarrow’, A, B, ‘String’, str)

[Passing the above points to annotation function. Please note that first argument in this case is ‘textarrow’.

We have also passed the string with message to be displayed]

Input:

Fs = 0:pi/50:2*pi;
y = sin(Fs);
plot(Fs,y)
A = [0.3 0.3];
B = [0.8 0.9];
messageToDisplay = 'First Maxima'
annotation('textarrow', A, B, 'String', messageToDisplay)

Output:

As we can see in the output, the first peak of the sine wave is being pointed and we also have a text message displayed.

Example #5

In this example, we will use the textbox annotation which is a shapetype annotation.

Code:

Fs = 0:pi/50:2*pi;

[Defining the frequency for sinewave]

y = sin(Fs);

[Initializing the sine wave]

plot(Fs,y)

[Creating the plot of sine wave]

boxDimension = [0.5 0.5 0.3 0.3];

[Defining the points for the annotation]

messageToDisplay = ‘Let us learn annotation’

[Initializing the string with text message]

annotation(‘textbox’, boxDimension, ‘String’, messageToDisplay, ‘FitBoxToText’, ‘on’);

[Passing the above points to annotation function. Please note that first argument in this case is ‘textbox’]

Input:

Fs = 0:pi/50:2*pi;
y = sin(Fs);
plot(Fs,y)
boxDimension = [0.5 0.5 0.3 0.3];
messageToDisplay = 'Let us learn annotation'
annotation('textbox', boxDimension, 'String', messageToDisplay, 'FitBoxToText', 'on');

Output:

As we can see in the output, our plot has an annotation in the form of a text box with required message.

To get a simple rectangle as annotation, just change the argument from ‘textbox’ to ‘rectangle’ in the above code and remove other arguments.

Conclusion

Annotation is done to make our plot more readable and intuitive. Any additional information that we want the reader to have about our graph can be passed as annotation. Matlab provides us with various annotation types like line, arrow, textbox etc.

Recommended Articles

This is a guide to Matlab Annotation. Here we discuss the introduction to Matlab Annotation along with programming examples. You may also have a look at the following articles to learn more –

  1. Matlab sym()
  2. xlsread Matlab
  3. xlabel Matlab
  4. MATLAB Indexing

The post Matlab Annotation appeared first on EDUCBA.



This post first appeared on Best Online Training & Video Courses | EduCBA, please read the originial post: here

Share the post

Matlab Annotation

×

Subscribe to Best Online Training & Video Courses | Educba

Get updates delivered right to your inbox!

Thank you for your subscription

×