Labels, title, and legend in MATLAB - Time & Space Complexity
We want to understand how the time it takes to add labels, titles, and legends to a plot changes as the plot gets more complex.
How does adding these elements affect the program's running time?
Analyze the time complexity of the following code snippet.
x = 1:1000;
y = sin(x);
plot(x, y);
title('Sine Wave');
xlabel('X Axis');
ylabel('Y Axis');
legend('sin(x)');
This code plots a sine wave and adds a title, labels for the x and y axes, and a legend.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Plotting the data points (drawing 1000 points).
- How many times: The plot function processes all 1000 points once.
- Adding title, labels, and legend are single operations without loops.
Adding labels, title, and legend takes about the same time no matter how many points are plotted.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Plotting 10 points + fixed label/title/legend steps |
| 100 | Plotting 100 points + fixed label/title/legend steps |
| 1000 | Plotting 1000 points + fixed label/title/legend steps |
Pattern observation: Plotting time grows with number of points, but adding labels, title, and legend stays constant.
Time Complexity: O(n)
This means the time grows linearly with the number of points plotted, while adding labels, title, and legend adds only a small fixed time.
[X] Wrong: "Adding a title or legend makes the program run much slower as data grows."
[OK] Correct: Titles and legends are added once and do not depend on data size, so their time cost stays about the same.
Understanding which parts of code grow with input size helps you explain performance clearly and shows you can think about efficiency in real projects.
"What if we added a legend entry for every data point? How would the time complexity change?"
Practice
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]
- Confusing xlabel with ylabel
- Using title instead of axis labels
- Using legend for axis labels
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]
- Using assignment '=' instead of function call
- Using square or curly brackets
- Missing quotes around labels
plot(1:5, [2 4 6 8 10]);
title('Sales Over Time');
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]
- Confusing title with axis labels
- Expecting variable values as title
- No title if title function missing
plot(x, y);
xlabel = 'Time';
ylabel('Value');
legend('Line1');
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]
- Assigning xlabel instead of calling function
- Confusing ylabel syntax
- Incorrect legend usage
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'?
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]
- Assigning strings instead of calling functions
- Using brackets or braces for legend
- Missing quotes around labels
Ready to prove it?
Pass the test and this pattern moves unknown
