subplot syntax in matlab

To create multiple plots in the same figure window, we use the subplot function in MATLAB. The basic syntax for subplot function is as follows:

main.m
subplot(m,n,p)
15 chars
2 lines

Where,

  • m : number of rows in the plot grid
  • n : number of columns in the plot grid
  • p : position of the current plot in the plot grid

For example, to create a 2x2 plot grid and plot the first plot in the first cell, we would use:

main.m
% Create a 2x2 plot grid
subplot(2,2,1);

% Plot your data in the first plot
plot(x,y);
88 chars
6 lines

To plot the second plot in the same figure, we would use:

main.m
% Create a 2x2 plot grid
subplot(2,2,2);

% Plot your data in the second plot
plot(x,z);
89 chars
6 lines

And so on for the remaining plots. The subplot function allows us to create multiple plots easily and effectively manage the position of each plot in the same figure.

gistlibby LogSnag