0
0
MATLABdata~20 mins

Subplot for multiple panels in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Subplot Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this MATLAB subplot code?

Consider the following MATLAB code that creates subplots and plots simple lines. What will be the output displayed?

MATLAB
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');
ATwo plots stacked vertically: top plot shows y = x^2, bottom plot shows x versus y
BTwo plots stacked vertically: top plot shows y = x^2, bottom plot shows x vs y (same as top plot)
COne plot only showing y = x^2, bottom plot is empty
DError: subplot indices exceed matrix dimensions
Attempts:
2 left
💡 Hint

Remember that subplot(m,n,p) divides the figure into an m-by-n grid and activates the p-th subplot.

Predict Output
intermediate
2:00remaining
What happens if subplot indices overlap?

What will be the result of running this MATLAB code?

MATLAB
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');
ATwo separate subplots appear side by side
BBoth sine and cosine plots appear in the first subplot layered
CError: subplot index repeated
DOnly the cosine plot is visible in the first subplot; sine plot is overwritten
Attempts:
2 left
💡 Hint

Calling subplot with the same index activates the same panel, so new plots overwrite previous ones.

🔧 Debug
advanced
2:00remaining
Why does this subplot code produce an error?

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?

MATLAB
x = 1:10;
for i = 1:4
  subplot(3,1,i);
  plot(x, x*i);
  title(['Plot ' num2str(i)]);
end
AError because subplot index 4 exceeds the 3x1 grid size
BError because loop variable i is not valid for subplot
CNo error; plots appear in first three subplots, fourth plot ignored
DError because plot command syntax is incorrect
Attempts:
2 left
💡 Hint

Check the maximum subplot index allowed for a 3x1 grid.

🚀 Application
advanced
2:30remaining
How to create a 2x2 grid with one large subplot?

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?

Asubplot(2,2,1); plot(...); subplot(2,2,2); plot(...); subplot(2,2,3); plot(...);
Bsubplot(2,2,[1 2]); plot(...); subplot(2,2,3); plot(...); subplot(2,2,4); plot(...);
Csubplot(3,2,1); plot(...); subplot(3,2,3); plot(...); subplot(3,2,4); plot(...);
Dsubplot(1,3,1); plot(...); subplot(1,3,2); plot(...); subplot(1,3,3); plot(...);
Attempts:
2 left
💡 Hint

Use vector indexing in subplot to span multiple grid cells.

🧠 Conceptual
expert
3:00remaining
How does MATLAB handle subplot handles when reusing panels?

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?

AA new axes object is created and the handle changes; old plot remains unchanged
BMATLAB throws an error because subplot index is reused
CThe handle points to the existing axes; new plots overwrite the old content in that panel
DThe subplot command ignores the call and keeps the old plot unchanged
Attempts:
2 left
💡 Hint

Think about how MATLAB manages axes objects and figure panels.