📖 Scenario: You are analyzing sales data for two products over a week. You want to create a clear line plot to compare their sales trends.
🎯 Goal: Create a line plot with sales data for two products. Add labels for the x-axis and y-axis, a title for the plot, and a legend to identify each product's line.
📋 What You'll Learn
Create vectors for days and sales data for two products
Add x-axis label as 'Day of Week'
Add y-axis label as 'Sales Units'
Add a title 'Weekly Sales Comparison'
Add a legend with labels 'Product A' and 'Product B'
💡 Why This Matters
🌍 Real World
In business, clear charts help teams understand sales trends quickly and make better decisions.
💼 Career
Data analysts and scientists often create plots with labels, titles, and legends to communicate insights clearly.
Progress0 / 4 steps
1
Create sales data vectors
Create a vector called days with values from 1 to 7 representing days of the week. Create two vectors called salesA and salesB with these exact values: salesA = [50 60 55 70 65 80 75] and salesB = [45 55 60 65 70 75 85].
MATLAB
Hint
Use the colon operator to create days. Assign the exact arrays for salesA and salesB.
2
Plot the sales data
Use the plot function to plot salesA and salesB against days. Use different line styles or colors for each product. Use hold on to plot both lines on the same figure.
MATLAB
Hint
Plot salesA first, then use hold on before plotting salesB.
3
Add labels and title
Add an x-axis label with xlabel('Day of Week'), a y-axis label with ylabel('Sales Units'), and a title with title('Weekly Sales Comparison').
MATLAB
Hint
Use xlabel, ylabel, and title functions with the exact text.
4
Add a legend and display the plot
Add a legend with labels 'Product A' and 'Product B' using the legend function. Then use hold off to finish the plot. Finally, display the plot by running the script.
MATLAB
Hint
Use legend with the exact labels and then hold off to finish.
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
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.
Step 2: Identify the function for x-axis label
The function xlabel is specifically designed to add a label to the x-axis.
Final Answer:
xlabel -> Option A
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
Step 1: Recall correct legend syntax
In MATLAB, the function legend is 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 D
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
Step 1: Understand the title function
The title function sets the main heading of the plot to the given string.
Step 2: Check the given code
The code calls title('Sales Over Time'), so the plot's title will be exactly 'Sales Over Time'.
Final Answer:
Sales Over Time -> Option B
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: