LESSON: Getting Started with MATLAB

FOCUS QUESTION: How do I start using MATLAB?

This lesson takes you through the basic process of performing data analysis in MATLAB.

In this lesson you will:

  • Create a new lesson project.
  • Create a new script to hold your analysis.
  • Enter commands to do the data analysis.
  • Save the script and run it.
  • Publish the analysis.
Along the way you will also learn about cell mode and some simple debugging.
Image for San Antonio TransGuide cameras looking East at IH10 and De Zavala 8/21/12 at 8:02 am

Contents

DATA FOR THIS LESSON

File Description
count.dat This data file contains traffic measurements from three intersections over a 24-hour period. We assume that these correspond to the following San Antonio intersections:

  • Durango at N. St. Mary's
  • IH10 at De Zavala
  • LP410 at Military
The data is in text format.

SETUP FOR THE GETTING STARTED LESSON

  1. Create a GettingStarted directory under your V: drive.
  2. Create a GettingStartedLesson.m in the GettingStarted subdirectory.
  3. Enter the commands as described below into the GettingStartedLesson script file.

EXAMPLE 1: Read data from a file into MATLAB (load a text file)

Type the following in the lesson 1 script, save, and run:

   load count.dat;

You should see a count variable in your workspace:

EXAMPLE 2: Plot the data just read (simple plot in a new figure)

Type the following in the lesson 1 script, save, and run:

   figure
   plot(count)

You should see a Figure Window with an unlabeled plot of the traffic:

EXAMPLE 3: Plot the data in an informative way (labeled plot in new figure)

Type the following in the lesson 1 script, save, and run:

   figure
   plot(count)
   title('Traffic at 3 San Antonio intersections')
   xlabel('Hour of day (24 hour format)')
   ylabel('Number of vehicles')
   legend('Durango','DeZavala', 'LP410')

You should see a Figure Window with a labeled plot of the traffic:

EXAMPLE 4: Reorganize your script into cells (cell mode)

When you organize your script into cells, you can execute the cells individually. Published scripts with cells are organized into subsections for better readability.

Start a new cell before each of the three examples. Use the new cell icon Create a new cell at the cursor position or start a new line in your script with two percent signs (%%) followed by a space.

EXAMPLE 5: Publish your final script

Press the publish icon Save and publish script to save an publish your script

SUMMARY OF SYNTAX

MATLAB syntax Description
load myfile Read previously saved variables from the myfile file into MATLAB. The myfile file must be a text file or have a special format called the MAT-file format.
figure Create a new Figure Window, which then becomes the current figure.
plot(Y) Draw a separate x-y plot for each column of the array Y in the current Figure Window. If Y is a vector, plot draws a single line representing the vector. If Y has multiple columns, plot displays each column as a separate line. These values are plotted against the values 1, 2, 3, 4, ...
title('string') Insert string as a title above the plot of the current axis.
xlabel('string') Insert string as the label for the x-axis of the current axis.
ylabel('string') Insert string as the label for the y-axis of the current axis.
legend Add an identifying annotation to the current figure.
Execute entire script Run the script currently selected in the Editor Window by pressing this icon.
Create a new section(cell) at the cursor position Insert a section (cell) at the cursor position. Alternatively, type two percent signs (%%) followed by a space at the beginning of a line.
Execute section and stay in the section(cell) Execute a section (cell), keeping the cursor in the section after execution.
Execute section and move to the next section(cell) Execute a section (cell) and advance the cursor to the next section (for stepping through section).
Move to the next section(cell) Advance the cursor to the next section (for stepping through sections).

This lesson was written by Kay A. Robbins of the University of Texas at San Antonio and last modified by Dawn Roberson on 3-Jan-2014. Please contact Kay.Robbins@utsa.edu with comments or suggestions.