subplotting code in matlab

To create subplots in MATLAB, we can use the subplot(m,n,p) function. Here, m and n determine the number of rows and columns of the subplot grid, while p is used to specify which subplot we want to create.

For example, if we want to create a 2x2 grid of subplots and plot our data in the first one, we can use the following code:

main.m
x = 0:0.1:10;
y1 = sin(x);
y2 = cos(x);
y3 = atan(x);

subplot(2,2,1)
plot(x,y1)

subplot(2,2,2)
plot(x,y2)

subplot(2,2,3)
plot(x,y3)
135 chars
14 lines

This will create a grid of four subplots with the first plot in the top-left corner, the second plot in the top-right corner, and so on.

We can also customize each subplot individually using additional plotting commands.

main.m
subplot(2,2,1)
plot(x,y1,'LineWidth',2)
xlabel('X-axis')
ylabel('Y-axis')
title('Sine Plot')

subplot(2,2,2)
plot(x,y2,'-.r')
xlabel('X-axis')
ylabel('Y-axis')
title('Cosine Plot')

subplot(2,2,3)
plot(x,y3,'--k')
xlabel('X-axis')
ylabel('Y-axis')
title('Arctangent Plot')
273 chars
18 lines

This will create a grid of four subplots with each plot having its own unique line style, color and title.

related categories

gistlibby LogSnag