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.

In this lesson you will:
  • Use the plot function to make a simple graph.
  • Use the plot tools to add labels and captions.
  • Adjust the axes properties to improve the plot.
  • Save the result in a .fig file for future use.
  • Create a script to hold MATLAB commands.
  • Learn to use cell mode in a script.
Image for San Antonio TransGuide cameras looking East at IH10 and De Zavala


Contents

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:


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)


EXAMPLE 6: Use the script in cell mode (demonstrated in class)


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 Execute entire script icon in the Editor Window to run the script currently shown in that window.
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 Execute cell and stay in the cell or Execute cell and move to the next cell.


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.