📖 Scenario: You are creating a simple plot to show the relationship between two sets of data points. You want to control the axis limits and add labels to make the plot clear and easy to understand.
🎯 Goal: Build a MATLAB script that plots data points, sets specific axis limits, and adds axis labels with formatting.
📋 What You'll Learn
Create vectors for x and y data points
Set axis limits using axis
Add labels to x-axis and y-axis using xlabel and ylabel
Format axis labels with font size and font weight
💡 Why This Matters
🌍 Real World
Controlling axis limits and formatting labels is important when creating clear and professional graphs for reports, presentations, or data analysis.
💼 Career
Many jobs in engineering, science, and data analysis require creating plots that communicate data clearly. Knowing how to control axes and labels is a basic but essential skill.
Progress0 / 4 steps
1
Create data vectors
Create two vectors called x and y with these exact values: x = [1 2 3 4 5] and y = [2 4 6 8 10].
MATLAB
Hint
Use square brackets to create vectors in MATLAB, separate numbers with spaces.
2
Plot data and set axis limits
Use plot(x, y) to plot the data points. Then set the axis limits to [0 6 0 12] using the axis function.
MATLAB
Hint
Use plot(x, y) to draw the graph and axis([xmin xmax ymin ymax]) to set limits.
3
Add axis labels
Add an x-axis label with the text 'Time (seconds)' using xlabel. Add a y-axis label with the text 'Distance (meters)' using ylabel.
MATLAB
Hint
Use xlabel('text') and ylabel('text') to add labels.
4
Format axis labels
Set the font size of both axis labels to 14 and font weight to 'bold' using the set function with the label handles returned by xlabel and ylabel.
MATLAB
Hint
Store the output of xlabel and ylabel in variables, then use set to change properties.
Practice
(1/5)
1. What does the MATLAB command axis([0 10 0 5]) do to a plot?
easy
A. Sets the x-axis limits from 0 to 10 and y-axis limits from 0 to 5
B. Sets the axis color to RGB values [0 10 0 5]
C. Creates a grid with 10 rows and 5 columns
D. Labels the axes with numbers 0, 10, 0, and 5
Solution
Step 1: Understand the axis function input
The axis function with a vector of four numbers sets axis limits: [xmin xmax ymin ymax].
Step 2: Apply the given limits
Here, [0 10 0 5] means x-axis from 0 to 10 and y-axis from 0 to 5.
Final Answer:
Sets the x-axis limits from 0 to 10 and y-axis limits from 0 to 5 -> Option A
Quick Check:
axis([xmin xmax ymin ymax]) = set axis limits [OK]
Plot points with x-axis from 0 to 6, y-axis from 0 to 12, x-ticks at 0,2,4,6 and y-ticks at 0,6,12 -> Option A
Quick Check:
axis + xticks + yticks control limits and ticks [OK]
Hint: axis sets limits; xticks/yticks set tick marks [OK]
Common Mistakes:
Assuming default ticks remain after setting axis
Thinking xticks/yticks need same length as data
Confusing axis limits with tick positions
4. Identify the error in this MATLAB code snippet for setting axis limits and fix it:
plot(1:3, [5 10 15]) axis(0 3 0 20)
medium
A. Limits must be strings; use axis('0 3 0 20')
B. axis function does not set limits; use xlim and ylim instead
C. Missing square brackets around limits; should be axis([0 3 0 20])
D. No error; code runs fine
Solution
Step 1: Check axis function syntax
The axis function requires a vector input for limits, enclosed in square brackets.
Step 2: Identify missing brackets
The code uses axis(0 3 0 20) without brackets, causing syntax error.
Final Answer:
Missing square brackets around limits; should be axis([0 3 0 20]) -> Option C
Quick Check:
axis([xmin xmax ymin ymax]) needs brackets [OK]
Hint: Always use square brackets for axis limits [OK]
Common Mistakes:
Omitting brackets around axis limits
Using strings instead of numeric vectors
Confusing axis with xlim or ylim functions
5. You want to create a plot with x-axis from -5 to 5, y-axis from -10 to 10, and custom ticks every 2 units on both axes. Which MATLAB code correctly achieves this?