MATLAB Plotting: Creating Your First Graph

Introduction

MATLAB provides powerful tools for creating various types of plots and graphs. In this guide, we'll walk you through creating your first graph in MATLAB with sample code.

Basic Plotting

The most fundamental plot in MATLAB is a line plot. You can create one with the `plot` function. Here's an example:

``% Example: Create a simple line plotx = [1, 2, 3, 4, 5];y = [10, 20, 15, 30, 25];plot(x, y);xlabel('X-axis');ylabel('Y-axis');title('Sample Line Plot');    ``

MATLAB allows you to customize your plots by adding labels, titles, gridlines, and more. Here's an example:

``% Example: Customizing your plotx = [0, 1, 2, 3, 4];y = [0, 1, 4, 9, 16];plot(x, y, 'r-o');  % Red line with circles at data pointsxlabel('X-axis');ylabel('Y-axis');title('Customized Plot');grid on;    ``

Types of Plots

MATLAB supports various types of plots, including bar plots, scatter plots, histograms, and more. You can explore these plots for different data visualization needs.

You can save your plot to an image file using the `saveas` function. Here's an example:
``% Example: Export your plot to a PNG filex = 0:0.1:2*pi;y = sin(x);plot(x, y);xlabel('X-axis');ylabel('Y-axis');title('Sine Wave');grid on;saveas(gcf, 'sine_wave_plot.png');    ``