0
0
MATLABdata~10 mins

Subplot for multiple panels in MATLAB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Subplot for multiple panels
Start
Define subplot grid
Select panel 1
Plot data in panel 1
Select panel 2
Plot data in panel 2
...repeat for all panels...
Display all panels
End
This flow shows how MATLAB creates multiple panels by defining a grid, selecting each panel, plotting data, and displaying all panels together.
Execution Sample
MATLAB
x = 1:5;
subplot(2,2,1);
plot(x, x);
subplot(2,2,2);
plot(x, x.^2);
This code creates a 2x2 grid of plots and draws a line and a parabola in the first two panels.
Execution Table
StepActionPanel SelectedPlot CommandResult
1Define x as 1 to 5--x = [1 2 3 4 5]
2Select subplot panel 1Panel 1 (top-left)-Panel 1 ready for plotting
3Plot x vs xPanel 1plot(x, x)Line plot in panel 1
4Select subplot panel 2Panel 2 (top-right)-Panel 2 ready for plotting
5Plot x vs x squaredPanel 2plot(x, x.^2)Parabola plot in panel 2
6No more panels selected--Display shows 2 plots in 2 panels of 2x2 grid
💡 All specified panels plotted, execution ends.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
xundefined[1 2 3 4 5][1 2 3 4 5][1 2 3 4 5][1 2 3 4 5]
Current PanelnonenonePanel 1Panel 2Panel 2
Key Moments - 3 Insights
Why does the plot appear only in the selected panel?
Because subplot sets the current panel for plotting. Each plot command draws only in the active panel, as shown in steps 2 and 4 of the execution_table.
What happens if you call subplot with the same panel number twice?
The second call selects that panel again, so the new plot overwrites the old one in that panel. This is why selecting a panel before plotting is important (see steps 2 and 4).
Why do we see empty panels if we define a 2x2 grid but plot only in two panels?
Because subplot creates the grid layout, but only panels with plots show data. The other panels remain empty but visible (step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'Current Panel' after step 3?
APanel 2
BPanel 1
Cnone
DPanel 3
💡 Hint
Check the 'Current Panel' row in variable_tracker after step 3.
At which step does the parabola plot appear?
AStep 2
BStep 3
CStep 5
DStep 6
💡 Hint
Look at the 'Plot Command' and 'Result' columns in execution_table.
If you add subplot(2,2,3) and plot(x, x.^3) after step 5, what changes in variable_tracker?
AA new 'Current Panel' value 'Panel 3' appears after step 6
BVariable x changes values
CNo change in 'Current Panel'
Dx becomes undefined
💡 Hint
Adding a new subplot changes the current panel selection as tracked in variable_tracker.
Concept Snapshot
subplot(m,n,p) creates a grid of m rows and n columns.
Select panel p to plot in that panel.
Each plot command draws only in the selected panel.
Panels not plotted remain empty but visible.
Use subplot repeatedly to fill multiple panels.
Full Transcript
This lesson shows how MATLAB's subplot function divides a figure into multiple panels arranged in a grid. We start by defining a vector x from 1 to 5. Then we select the first panel in a 2 by 2 grid and plot x versus x, which draws a line. Next, we select the second panel and plot x versus x squared, drawing a parabola. The variable tracker shows how the current panel changes with each subplot call. Key points include that each plot draws only in the selected panel, and empty panels remain visible if not plotted. The quiz tests understanding of panel selection and plotting order.