Labels, titles, and legends help explain what a graph shows. They make charts easier to understand.
Labels, title, and legend in MATLAB
Introduction
Syntax
MATLAB
xlabel('x-axis label') ylabel('y-axis label') title('Graph title') legend('label1', 'label2', ...)
Use xlabel and ylabel to name the axes.
title adds a heading to the plot.
legend shows labels for multiple data series.
Examples
MATLAB
xlabel('Time (seconds)') ylabel('Speed (m/s)') title('Speed over Time')
MATLAB
plot(x1, y1, x2, y2) legend('Car A', 'Car B')
MATLAB
xlabel('Temperature (°C)') title('Temperature Changes')
Sample Program
This program plots sine and cosine curves on the same graph. It labels the x-axis as 'Angle (radians)', the y-axis as 'Value', adds a title, and a legend to distinguish the two lines.
MATLAB
x = 0:0.1:2*pi; y1 = sin(x); y2 = cos(x); plot(x, y1, '-r', x, y2, '-b') xlabel('Angle (radians)') ylabel('Value') title('Sine and Cosine Functions') legend('sin(x)', 'cos(x)')
Important Notes
Always add labels and titles to make your plots clear.
Use legends when you have more than one data series in a plot.
You can customize font size and style using additional properties if needed.
Summary
Labels name the axes to explain what data they show.
Titles give the graph a clear heading.
Legends identify multiple data sets on the same plot.
Practice
1. Which MATLAB function is used to add a label to the x-axis of a plot?
easy
Solution
Step 1: Understand axis labeling functions
In MATLAB,xlabellabels the x-axis,ylabellabels the y-axis,titleadds a main heading, andlegendexplains plot lines.Step 2: Identify the function for x-axis label
The functionxlabelis specifically designed to add a label to the x-axis.Final Answer:
xlabel -> Option AQuick 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
Solution
Step 1: Recall correct legend syntax
In MATLAB, the functionlegendis called with parentheses and string arguments to label plot lines.Step 2: Identify correct syntax
The correct syntax uses parentheses and comma-separated strings:legend('Data1', 'Data2'). Other options use invalid assignment or brackets.Final Answer:
legend('Data1', 'Data2') -> Option DQuick 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
Solution
Step 1: Understand the title function
Thetitlefunction sets the main heading of the plot to the given string.Step 2: Check the given code
The code callstitle('Sales Over Time'), so the plot's title will be exactly 'Sales Over Time'.Final Answer:
Sales Over Time -> Option BQuick 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
Solution
Step 1: Check xlabel usage
The code usesxlabel = 'Time';which assigns a string to variable xlabel instead of calling the functionxlabel('Time').Step 2: Verify other lines
ylabel('Value')andlegend('Line1')are correctly called as functions. The plot function has argumentsx, yassumed defined.Final Answer:
Using assignment '=' with xlabel instead of function call -> Option AQuick 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:
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'?
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
Solution
Step 1: Use correct function calls for labels and title
Functionstitle,xlabel, andylabelrequire parentheses with string arguments.Step 2: Use correct legend syntax
Thelegendfunction takes comma-separated strings inside parentheses, not assignment or brackets.Final Answer:
title('Comparison'); xlabel('Time'); ylabel('Value'); legend('Data1', 'Data2'); -> Option CQuick 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
10 questions ~5 mineasy hard
