two seperate graphs in one figure in matlab

To create two separate graphs (or plots) in one figure window in Matlab, you can make use of the subplot() function, which allows you to create multiple axes inside a single figure.

Here's an example code snippet that demonstrates how you can use subplot() to create two plots side by side:

main.m
% Create some sample data
x = 0:0.1:10;
y1 = sin(x);
y2 = cos(x);

% Create a new figure window
figure

% Create the first subplot
subplot(1,2,1) % 1 row, 2 columns, first plot
plot(x,y1)
title('Sin(x)')

% Create the second subplot
subplot(1,2,2) % 1 row, 2 columns, second plot
plot(x,y2)
title('Cos(x)')
307 chars
18 lines

In this code snippet, we first create some sample data (two sine waves of different frequencies). Then, we create a new figure window using the figure function.

Next, we create two subplots using the subplot function. The first argument to subplot() specifies the number of rows in the subplot grid, the second argument specifies the number of columns, and the third argument specifies which subplot to create (counting from left to right and top to bottom).

In this case, we pass 1,2,1 to subplot() to create the first subplot on the left-hand side of the figure, and 1,2,2 to create the second subplot on the right-hand side.

Finally, we use the plot() function to plot the sample data in each subplot, and add a title to each plot using the title() function.

gistlibby LogSnag