To add text annotations to a graph in MATLAB, you can use the `text` function to specify the position and content of the text.
text(x, y, 'Your text here', 'FontSize', 12, 'Color', 'red');
Getting Started with MATLAB Graphics
Overview of MATLAB graphing capabilities
MATLAB provides robust tools for data visualization, allowing users to create various types of graphs, including 2D plots, 3D plots, and more. Whether you need to visualize mathematical functions, experimental data, or simulations, understanding how to create and annotate graphs is essential.
Setting up your environment
Before diving into adding text to your graphs, ensure you have a working installation of MATLAB. Familiarize yourself with the basic commands for plotting by practicing with sample datasets available in MATLAB.

Basic Text Annotations
Using `text` Function
The `text` function in MATLAB is fundamental for adding annotations directly onto graphs. It allows you to place text at specified coordinates within your plot.
Syntax and parameters of the `text` function:
text(x, y, 'String', 'PropertyName', PropertyValue, ...)
Here, `(x, y)` specifies the position of the text, `'String'` is the content you want to display, and properties like FontSize and Color customize its appearance.
Example 1: Adding simple text to a graph
x = 0:0.1:10;
y = sin(x);
plot(x, y);
text(5, 0, 'Midpoint', 'FontSize', 12, 'Color', 'red');
In this example, we plot a sine wave and add the text "Midpoint" at the coordinates (5, 0). The text is customized to appear in red at a font size of 12, enhancing the graph's readability and highlighting significant points.
Positioning Text
Understanding how to position your text effectively is crucial for clarity. You can specify exact coordinates or use various positioning options.
Example 2: Custom positioning of text
plot(x, y);
text(3, 0.5, 'Custom Position', 'Units', 'normalized', 'Position', [0.5, 0.5]);
In this example, we use normalized units to place the text at the center of the plot area. This approach allows for better adaptability when resizing figures or working with multiple datasets.

Adding Titles and Labels
Adding Titles to Figures
Clear titles are essential for understanding what the plot represents. The `title` function allows you to add a descriptive title to your graph easily.
Example 3: Custom title
plot(x, y);
title('Sine Wave');
In the above code, we title our sine wave graph "Sine Wave." This title conveys the essential information right away and sets the context for any viewer.
X and Y Axis Labels
To provide further clarity, labeling the x-axis and y-axis is vital. Use `xlabel` and `ylabel` functions to accomplish this.
Example 4: Labeling axes
xlabel('X values');
ylabel('Sine of X');
Adding these labels not only improves readability but also enhances the viewer’s understanding of the graph's dimensions and data representation.

Using Legends for Clarity
Creating Legends
Legends are crucial for distinguishing multiple datasets in a single plot. The `legend` function automatically creates an annotated box that explains what each line represents.
Example 5: Adding a legend
plot(x, y, 'DisplayName', 'sin(x)');
hold on;
plot(x, cos(x), 'DisplayName', 'cos(x)');
legend show;
In this example, we plot both the sine and cosine functions and create a legend that helps viewers quickly identify each curve. The `DisplayName` property assigns a label that appears in the legend.

Advanced Text Customization
Font Customization
Beyond positioning, you can customize the text's appearance significantly. Font size, style, and color can dramatically enhance the visual impact of your annotations.
Example 6: Customizing text appearance
text(5, 0, 'Styled Text', 'FontSize', 14, 'FontWeight', 'bold', 'Color', 'blue');
Here, we create styled text that is bold and blue, set at a font size of 14. Such customizations not only draw attention but also improve the aesthetics of your graph.
Using Multiple Lines of Text
To add more information, you can also create multi-line text annotations. The newline character (`\n`) allows you to format your text into multiple lines.
Example 7: Multi-line text
str = 'This is line 1\nThis is line 2';
text(5, 0, str, 'Interpreter', 'latex');
In this instance, we utilize LaTeX to format our multi-line text. This feature is particularly useful for conveying complex information without cluttering your graph.

Using LaTeX for Text Formatting
Introduction to LaTeX Interpreter
MATLAB supports LaTeX formatting in text annotations, enabling you to create professional-looking graphs with mathematical symbols, equations, and custom styles.
Example 8: LaTeX formatted text
text(5, 0, '$$\sin(x)$$', 'Interpreter', 'latex', 'FontSize', 16);
This code uses the LaTeX interpreter to display the sine function symbolically, offering an advanced and elegant way to present mathematical concepts.

Conclusion
Adding text to your MATLAB graphs is not just about aesthetic enhancement—it significantly improves clarity and understanding of your data. Whether you’re labeling axes, adding legends, or incorporating styled text, effective annotations can turn a basic plot into a powerful communication tool. Never hesitate to experiment with various formatting options to achieve the best possible representation of your data.

Additional Resources
To deepen your knowledge of MATLAB, refer to the official MATLAB documentation and explore related tutorials on graphical data visualization. If you have questions or need tailored assistance, feel free to reach out through our contact channels. Your journey in mastering MATLAB starts here!