LESSON 1: Editing plots
FOCUS QUESTION: How do I start using MATLAB?
This lesson guides you through the process of creating and labeling a graph. You will then write a MATLAB script to recreate your work from scratch.
Contents
- DATA FOR THIS LESSON
- EXAMPLE 1: Read data from a file into the MATLAB workspace
- EXAMPLE 2: Plot the data just read
- EXAMPLE 3: Adjust the plot and save the figure
- EXAMPLE 4: Redisplay the saved figure
- EXAMPLE 5: Create a script to plot the traffic (demonstrated in class)
- EXAMPLE 6: Use the script in cell mode (demonstrated in class)
- SUMMARY OF SYNTAX
DATA FOR THIS LESSON
| File | Description |
count.dat |
contains traffic measurements from three intersections over a 24-hour period. We assume that these intersections are IH10 at De Zavala, LP410 at Military and Durango at N. St. Mary's in San Antonio. |
EXAMPLE 1: Read data from a file into the MATLAB workspace
Type the following in the Command Window:
load count.dat;
You should see a count variable in your workspace:
EXAMPLE 2: Plot the data just read
Type the following in the Command Window:
figure plot(count);
You should see a Figure Window with an unedited plot of the traffic:
EXAMPLE 3: Adjust the plot and save the figure
Do the following steps from the Figure Window:
- Use Tools->Edit Plot from the Figure Window menubar to make the figure editable.
- Label the x and y axes.
- Put a title on the figure.
- Add a legend.
- Change the x-axis so that x-ticks are labeled at 6 am, noon, 6 pm, and midnight. Remove the other ticks.
- Use File->Save As from Figure Window menubar to save the figure in the directory Z:\working\MATLAB\Lesson1 as the file traffic.fig. (You will have to create a new directory for Lesson1.)
- Change the Current Directory of MATLAB to Z:\working\MATLAB\Lesson1. (Use the pulldown menu from the main MATLAB Window menubar.)
EXAMPLE 4: Redisplay the saved figure
Type the following in the Command Window:
open traffic.fig;
You should see a Figure Window with your edited plot of the traffic:
EXAMPLE 5: Create a script to plot the traffic (demonstrated in class)
- Use File->New->Blank M-File from the main MATLAB menubar to open a new Editor Window.
- Type the commands from examples 1 to 4 in this window (or you can just drag them from the Command History window).
- Save the resulting script as Lesson1Script.m. (Note: your current directory should be z:\working\MATLAB\Lesson1.)
- Run the script using the green run arrow icon in the Editor Window.
EXAMPLE 6: Use the script in cell mode (demonstrated in class)
- Edit the script you created in Example 5 so that each statement is in a separate cell. (Create a cell by starting a line with two percent signs followed by a blank.)
- Execute each cell separately using the icons on the Editor Window toolbar.
- We will use cell mode in all of our future lessons and labs.
SUMMARY OF SYNTAX
| MATLAB syntax | Description |
load myfile |
reads previously saved variables from the
myfile file into MATLAB. The myfile file must be in a
special format called the MAT-file format. |
figure |
creates a new Figure Window, which becomes the current figure. |
plot(Y) |
draws a separate line graph for each column of the
array Y in the current Figure Window.
If Y is a vector, plot draws a
single line graph of Y.
These values are plotted against the values
1, 2, 3, 4, ... |
open myfile |
calls an appropriate application to access the
myfile file.
For figure files (extension .fig),
MATLAB displays the file as a figure in a new window.
The open
command works with many different types of files. For example, MATLAB
calls Microsoft Word to open a Word document
(extension .doc). |
| runing a script | press the
|
| cell mode | allows you to execute a piece of a MATLAB script.
Start a new cell using two percent signs (%%)
followed by a blank. Then type the commands you want to be
in the cell. Execute the cell by pressing either
|
This lesson was written by Kay A. Robbins of the University of Texas at San Antonio and last modified on 19-Aug-2011. Please contact krobbins@cs.utsa.edu with comments or suggestions.