Bird
Raised Fist0
MATLABdata~10 mins

Labels, title, and legend in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Labels, title, and legend
Create plot data
↓
Plot data on graph
↓
Add x-axis label
↓
Add y-axis label
↓
Add title
↓
Add legend
↓
Display complete graph
This flow shows how to create a plot, then add labels for x and y axes, a title, and a legend step-by-step.
Execution Sample
MATLAB
x = 1:5;
y = x.^2;
plot(x,y)
xlabel('X values')
ylabel('Y values')
title('Square Numbers')
legend('y = x^2')
This code plots y = x squared, then adds labels to the axes, a title, and a legend.
Execution Table
StepActionEffect on PlotOutput/Display
1Create x and y data arraysData ready for plottingNo visible output
2Plot x vs yGraph with points/line appearsGraph window shows curve
3Add xlabel 'X values'X-axis labeledX-axis shows label 'X values'
4Add ylabel 'Y values'Y-axis labeledY-axis shows label 'Y values'
5Add title 'Square Numbers'Title added on topTitle 'Square Numbers' appears above plot
6Add legend 'y = x^2'Legend box appearsLegend box shows 'y = x^2'
7Display complete graphAll labels, title, legend visibleFinal plot with all annotations
💡 All labels, title, and legend added; plot fully annotated and displayed
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 7
xundefined[1 2 3 4 5][1 2 3 4 5][1 2 3 4 5]
yundefined[1 4 9 16 25][1 4 9 16 25][1 4 9 16 25]
Key Moments - 3 Insights
Why do we add xlabel and ylabel after plotting the data?
Because the plot must exist first to know where to place the labels; see execution_table steps 2 and 3.
What happens if we forget to add a legend?
The plot will show the data but no description of what the line or points represent; see execution_table step 6.
Can we add multiple legends for multiple lines?
Yes, by passing multiple strings to legend() matching each plotted line; this example shows one legend for one line.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what label is added at step 4?
AY-axis label
BX-axis label
CTitle
DLegend
💡 Hint
Check the 'Action' column at step 4 in execution_table
At which step does the plot first become visible?
AStep 1
BStep 2
CStep 3
DStep 6
💡 Hint
Look at 'Effect on Plot' column to see when graph appears
If we remove the legend command, what changes in the final plot?
ANo title will show
BNo axis labels will show
CNo legend box will appear
DPlot will not display
💡 Hint
Refer to execution_table step 6 and 7 about legend presence
Concept Snapshot
plot(x,y) - draws the graph
xlabel('text') - labels x-axis
ylabel('text') - labels y-axis
title('text') - adds title
legend('label') - adds legend box
Add these after plotting for clear graphs.
Full Transcript
This example shows how to plot data in MATLAB and add labels, title, and legend. First, we create x and y data arrays. Then we plot y versus x. After the plot appears, we add an x-axis label with xlabel, a y-axis label with ylabel, a title with title, and a legend with legend. Each step updates the graph visually. The legend describes the plotted data line. Adding these elements helps make the graph easy to understand. The execution table traces each step and its effect on the plot. The variable tracker shows how x and y values remain the same after creation. Key moments clarify why labels come after plotting and the importance of the legend. The quiz tests understanding of when labels appear and what happens if the legend is missing.

Practice

(1/5)
1. Which MATLAB function is used to add a label to the x-axis of a plot?
easy
A. xlabel
B. ylabel
C. title
D. legend

Solution

  1. Step 1: Understand axis labeling functions

    In MATLAB, xlabel labels the x-axis, ylabel labels the y-axis, title adds a main heading, and legend explains plot lines.
  2. Step 2: Identify the function for x-axis label

    The function xlabel is specifically designed to add a label to the x-axis.
  3. Final Answer:

    xlabel -> Option A
  4. Quick Check:

    Label x-axis = xlabel [OK]
Hint: Use xlabel for x-axis label, ylabel for y-axis [OK]
Common Mistakes:
  • Confusing xlabel with ylabel
  • Using title instead of axis labels
  • Using legend for axis labels
2. Which of the following is the correct syntax to add a legend to a plot with two lines labeled 'Data1' and 'Data2'?
easy
A. legend{'Data1', 'Data2'}
B. legend = ('Data1', 'Data2')
C. legend['Data1', 'Data2']
D. legend('Data1', 'Data2')

Solution

  1. Step 1: Recall correct legend syntax

    In MATLAB, the function legend is called with parentheses and string arguments to label plot lines.
  2. Step 2: Identify correct syntax

    The correct syntax uses parentheses and comma-separated strings: legend('Data1', 'Data2'). Other options use invalid assignment or brackets.
  3. Final Answer:

    legend('Data1', 'Data2') -> Option D
  4. Quick Check:

    legend('labels') uses parentheses [OK]
Hint: Use legend('label1', 'label2') with parentheses [OK]
Common Mistakes:
  • Using assignment '=' instead of function call
  • Using square or curly brackets
  • Missing quotes around labels
3. What will be the title of the plot after running this MATLAB code?
plot(1:5, [2 4 6 8 10]);
title('Sales Over Time');
medium
A. plot(1:5, [2 4 6 8 10])
B. Sales Over Time
C. 1:5
D. No title

Solution

  1. Step 1: Understand the title function

    The title function sets the main heading of the plot to the given string.
  2. Step 2: Check the given code

    The code calls title('Sales Over Time'), so the plot's title will be exactly 'Sales Over Time'.
  3. Final Answer:

    Sales Over Time -> Option B
  4. Quick Check:

    title('text') sets plot title [OK]
Hint: title('text') sets the plot's main heading [OK]
Common Mistakes:
  • Confusing title with axis labels
  • Expecting variable values as title
  • No title if title function missing
4. Identify the error in this MATLAB code snippet:
plot(x, y);
xlabel = 'Time';
ylabel('Value');
legend('Line1');
medium
A. Using assignment '=' with xlabel instead of function call
B. Missing ylabel function call
C. legend syntax is incorrect
D. plot function is missing arguments

Solution

  1. Step 1: Check xlabel usage

    The code uses xlabel = 'Time'; which assigns a string to variable xlabel instead of calling the function xlabel('Time').
  2. Step 2: Verify other lines

    ylabel('Value') and legend('Line1') are correctly called as functions. The plot function has arguments x, y assumed defined.
  3. Final Answer:

    Using assignment '=' with xlabel instead of function call -> Option A
  4. Quick Check:

    Use xlabel('text'), not xlabel = 'text' [OK]
Hint: Call xlabel as function: xlabel('text'), not assignment [OK]
Common Mistakes:
  • Assigning xlabel instead of calling function
  • Confusing ylabel syntax
  • Incorrect legend usage
5. You have plotted two data sets on the same graph using:
plot(x, y1, '-r', x, y2, '-b');

How do you add a title 'Comparison', label the x-axis as 'Time', the y-axis as 'Value', and add a legend with labels 'Data1' and 'Data2'?
hard
A. title = 'Comparison'; xlabel = 'Time'; ylabel = 'Value'; legend = ('Data1', 'Data2');
B. title('Comparison'); xlabel('Time'); ylabel('Value'); legend(['Data1', 'Data2']);
C. title('Comparison'); xlabel('Time'); ylabel('Value'); legend('Data1', 'Data2');
D. title('Comparison'); xlabel('Time'); ylabel('Value'); legend{'Data1', 'Data2'};

Solution

  1. Step 1: Use correct function calls for labels and title

    Functions title, xlabel, and ylabel require parentheses with string arguments.
  2. Step 2: Use correct legend syntax

    The legend function takes comma-separated strings inside parentheses, not assignment or brackets.
  3. Final Answer:

    title('Comparison'); xlabel('Time'); ylabel('Value'); legend('Data1', 'Data2'); -> Option C
  4. Quick Check:

    Use function calls with parentheses for labels and legend [OK]
Hint: Use function calls with parentheses for all labels and legend [OK]
Common Mistakes:
  • Assigning strings instead of calling functions
  • Using brackets or braces for legend
  • Missing quotes around labels
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard