Bird
0
0

Identify the error in this MATLAB code:

medium📝 Debug Q6 of 15
MATLAB - 2D Plotting
Identify the error in this MATLAB code:
plot(1:4, [10 20 30 40]);
title('Sales Data');
xlabel('Month');
legend('Sales');
AMissing ylabel() for y-axis label
BNo error, code is correct
Clegend() called with only one label for one line
Dxlabel() should be ylabel()
Step-by-Step Solution
Solution:
  1. Step 1: Verify each line of code

    plot() creates the plot, title() adds title, xlabel() labels x-axis, legend('Sales') correctly labels the single plotted line.
  2. Step 2: Confirm no syntax or logical errors

    All functions are used correctly; missing ylabel() is optional and does not cause an error.
  3. Final Answer:

    No error, code is correct -> Option B
  4. Quick Check:

    Single line plot with legend('Sales') runs fine [OK]
Quick Trick: legend('label') is valid for single plots [OK]
Common Mistakes:
  • Thinking missing ylabel causes error
  • Thinking legend() requires multiple labels
  • Misusing legend() for axis labels

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes