Coding Ref

MATLAB subplot

MATLAB subplot

In MATLAB, a subplot is a way to display multiple plots on a single figure window. The syntax for creating a subplot in MATLAB is:

subplot(m, n, p)

Where m and n are the number of rows and columns of subplots, respectively, and p is the specific subplot number. For example, the command subplot(2, 2, 1) will create a figure window with two rows and two columns of subplots, and it will create the first subplot (upper left).

To create multiple subplots in a single figure window, you can use the subplot command multiple times. For example, to create four subplots arranged in a 2x2 grid, you could use the following commands:

subplot(2, 2, 1)
subplot(2, 2, 2)
subplot(2, 2, 3)
subplot(2, 2, 4)

Once you have created the subplots, you can use the usual plotting commands (such as plot, scatter, etc.) to plot data in each subplot. For example, to plot a sine wave in the first subplot and a cosine wave in the second subplot, you could use the following commands:

x = 0:0.1:10;
subplot(2, 2, 1)
plot(x, sin(x))
subplot(2, 2, 2)
plot(x, cos(x))

You can also customize the appearance of your subplots, such as the title, axis labels, and legend. For example, to add a title and axis labels to the first subplot, you could use the following commands:

subplot(2, 2, 1)
plot(x, sin(x))
title('Sine Wave')
xlabel('x')
ylabel('sin(x)')

Conclusion

In MATLAB, a subplot is a way to display multiple plots on a single figure window.

You'll also like

Related tutorials curated for you

    Plot markers in MATLAB

    How to plot points in MATLAB

    MATLAB subplot

    How to plot colors in MATLAB

    How to plot a line in MATLAB

    How to plot a legend in MATLAB

    How to create a 3D plot in MATLAB

    How to create a surface plot in MATLAB

    How to create a scatter plot in MATLAB

    How to plot a Bode in MATLAB

    MATLAB linspace