Consider the following MATLAB code that creates subplots and plots simple lines. What will be the output displayed?
x = 1:5; y = x.^2; subplot(2,1,1); plot(x,y); title('Top plot'); subplot(2,1,2); plot(y,x); title('Bottom plot');
Remember that subplot(m,n,p) divides the figure into an m-by-n grid and activates the p-th subplot.
The code creates a 2-row, 1-column grid of subplots. The first subplot (top) plots y = x^2. The second subplot (bottom) plots x versus y, which is the inverse relationship. Both plots appear stacked vertically.
What will be the result of running this MATLAB code?
x = 0:0.1:2*pi; y1 = sin(x); y2 = cos(x); subplot(2,2,1); plot(x,y1); title('Sine'); subplot(2,2,1); plot(x,y2); title('Cosine');
Calling subplot with the same index activates the same panel, so new plots overwrite previous ones.
The second subplot(2,2,1) call activates the same subplot as the first. The cosine plot overwrites the sine plot, so only cosine is visible.
Examine the MATLAB code below. It is intended to create a 3x1 subplot and plot data in each panel. Why does it produce an error?
x = 1:10; for i = 1:4 subplot(3,1,i); plot(x, x*i); title(['Plot ' num2str(i)]); end
Check the maximum subplot index allowed for a 3x1 grid.
The subplot grid has 3 panels (indices 1 to 3). Using index 4 causes an error because it is outside the grid.
You want to create a 2x2 subplot grid in MATLAB where the first subplot spans the entire top row, and the other two subplots are below it side by side. Which code achieves this layout?
Use vector indexing in subplot to span multiple grid cells.
Using subplot(2,2,[1 2]) merges the first and second subplot positions into one large subplot spanning the top row. The other two subplots fill the bottom row.
In MATLAB, when you create subplots and save their handles, what happens if you call subplot again with an existing panel index? How does this affect the handle and the plot?
Think about how MATLAB manages axes objects and figure panels.
Calling subplot with an existing panel index activates that axes. The handle points to the same axes, and new plots overwrite the previous content.