subplot is used in matlab to create multiple subplots in the same figure. You can draw a different graph in each subplot by specifying the number of rows, columns, and current plot position of the subplot.
In MATLAB, the subplot function is used to create multiple subplots in the same figure. Its basic syntax is:
subplot(m,n,p)
Among them, m and n represent the number of rows and columns of the subgraph respectively, and p represents the position of the current subgraph. The subplot function divides the current figure into a matrix of m rows and n columns, and sets the current drawing position to the p-th subfigure. The following will introduce in detail how to use the subplot function.
First, we need to create a figure window, which can be created using the figure function:
figure
Then, we can divide the subplots through the subplot function. For example, if we want to create a subgraph matrix with 2 rows and 2 columns, and set the current drawing position to the 1st subgraph, we can use the following code:
subplot(2,2,1)
Next, we can Draw the graph in the figure. For example, we can use the plot function to draw a simple curve:
x = 0:0.1:2*pi; y = sin(x); plot(x,y)
Then, we can continue to create other subplots. For example, we can set the current drawing position to the 2nd subgraph and draw another curve in it:
subplot(2,2,2) plot(x,cos(x))
Similarly, we can draw different graphics in other subgraphs. For example, we can set the current drawing position to the 3rd subplot and draw a scatter plot in it:
subplot(2,2,3) x = rand(1,100); y = rand(1,100); scatter(x,y)
Finally, we can set the current drawing position to the 4th subplot and draw in it A histogram:
subplot(2,2,4) x = 1:5; y = [3 5 2 6 1]; bar(x,y)
Through these steps, we can create multiple subgraphs in the same figure window and draw different graphics in each subgraph. It is worth noting that the parameters m, n and p of the subplot function must satisfy p<=m*n, otherwise an error will occur. In addition, if we only want to draw a subplot, we can use the plot function directly instead of using the subplot function.
In short, the subplot function is a function in MATLAB used to create multiple subplots in the same figure. We can draw different graphics in each subplot by specifying the number of rows, columns, and current plot position of the subplot.
The above is the detailed content of How to use subplot in matlab. For more information, please follow other related articles on the PHP Chinese website!