Bird
Raised Fist0
MATLABdata~20 mins

Labels, title, and legend in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
MATLAB Plot Label Master
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 plot labeling code?
Consider the following MATLAB code that plots a simple line and adds labels and a title. What text will appear as the x-axis label?
MATLAB
x = 1:5;
y = x.^2;
plot(x, y);
xlabel('Time (s)');
ylabel('Distance (m)');
title('Distance vs Time');
ATime (s)
BDistance (m)
CDistance vs Time
Dx-axis
Attempts:
2 left
💡 Hint
Look for the function that sets the label for the x-axis.
❓ Predict Output
intermediate
2:00remaining
What legend text appears in this MATLAB plot?
Given this MATLAB code, what text will appear in the legend?
MATLAB
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
plot(x, y1, '-r', x, y2, '-b');
legend('Sine Wave', 'Cosine Wave');
Asin and cos
BSine Wave and Cosine Wave
CRed and Blue lines
DSine and Cosine
Attempts:
2 left
💡 Hint
Check the legend function arguments.
🔧 Debug
advanced
2:00remaining
Why does this MATLAB code not display the title?
This MATLAB code plots data and tries to add a title, but the title does not appear. What is the problem?
MATLAB
x = 1:10;
y = rand(1,10);
plot(x,y)
title = 'Random Data Plot';
AThe plot command is missing a semicolon
BThe x and y vectors have different lengths
CThe y data is not sorted
DThe title is assigned to a variable instead of calling the title() function
Attempts:
2 left
💡 Hint
Check how the title is set in MATLAB.
🧠 Conceptual
advanced
2:00remaining
What happens if you call legend without arguments after plotting multiple lines?
In MATLAB, after plotting multiple lines, what does calling legend() with no arguments do?
AIt automatically creates a legend using the DisplayName properties of plotted lines
BIt clears any existing legend from the plot
CIt creates an empty legend box
DIt causes a syntax error
Attempts:
2 left
💡 Hint
Think about how MATLAB uses DisplayName properties in plots.
❓ Predict Output
expert
3:00remaining
What is the output of this MATLAB code with multiple labels and legend?
Analyze this MATLAB code and determine what text appears as the y-axis label and what the legend shows.
MATLAB
x = linspace(0,1,5);
y1 = x;
y2 = x.^2;
plot(x,y1,'-o', x,y2,'-s');
ylabel('Value');
legend({'Linear','Quadratic'});
title('Comparison Plot');
Ay-axis label: 'Comparison Plot'; legend: 'Linear' and 'Quadratic'
By-axis label: 'Value'; legend: 'y1' and 'y2'
Cy-axis label: 'Value'; legend: 'Linear' and 'Quadratic'
Dy-axis label: 'Value'; legend: 'o' and 's'
Attempts:
2 left
💡 Hint
Check the ylabel and legend function arguments carefully.

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