GLG410--Computers in Earth and Space Exploration

Announcements Syllabus Schedule Weekly lecture notes Assignments Links

Matlab scripting and simple data analysis


In this lecture, we will learn to create Matlab scripts ("M-files") that allow use to run a series of commands in a much quicker and easier way. We will then use this new tool to demonstrate how we can take a set of data from an input file, import that data into Matlab, have Matlab plot the data, and finally save it as a JPEG image, all in "one motion". The first part of this lecture is a demonstration of the usage and capabilities of scripting. The second part of class will be an in-class assignment where you can learn this new skill by playing with a dataset of a recent earthquake swarm in Yellowstone National Park.

How to access the ASU M-drive and set it as your working directory

Matlab Scripting

The advantage of "scripting" vs. "command line" is that you can give Matlab hundreds to thousands of commands by just typing the name of your saved script. Scripting in Matlab is done by using the Matlab Editor. The Editor is Matlab's built-in text editor. You will type the same kinds of commands that we have been typing on the command line. You will want to keep one command per line! You will also want to remember to end each line with a semicolon to suppress the output. Matlab scripts are also called "M-files". This is because they have the ".m" ending. Matlab will recognize ALL files with ".m" as scripts to run commands.

To open the Editor window, click the icon at the top of the Matlab GUI , or type edit. We can type the last commands from Lecture 3 into the Editor. We want to load the input file into Matlab, plot the river discharge data, add a title and axis labels, and then save the figure to a JPEG.

Save the script to an M-file. We can call this script daily_discharge.m. To run this script, type daily_discharge at the prompt in the Command Window.


The above script is functional, but if we were to pass this off to another person, it would take them some time to figure out what the script does if they run it. This is where commenting comes in VERY handy. To place comments within a script that you write, you will use the percent sign, %. All text in that line after % will turn green and will be a comment.

Here is that same script, but now it is properly commented.

Kevin's Tips For Proper Commenting Video explanation of scripting

The following two demos use the command line to illustrate the commands, but you should build these in scripts.
These examples use seismicity data from Bolivia. Here is the file for you to practice: seisformatlab.txt. The columns are: Date Latitude Longitude Depth Magnitude.

Video explanation of how to load data and make a map

Video explanation of how to load data and make a histogram

Assignment: Analysis of Yellowstone Seismicity data

At the end of 2008, Yellowstone National Park experienced an increase in seismic activity in the Yellowstone Lake area. We will take a look at this swarm of earthquakes, as well as others that have been recorded since 1973. You can find out more about this swarm

For this assignment, you will need to download the text file that includes the earthquake data from here.

The columns in the text file are as followed:

Main tasks:

  1. Write a script to import the data, and produce the following 2 plots
  2. Plot earthquakes as points, with Latitude on the vertical axis and Longitude on the horizontal axis
  3. Plot a histogram of number of earthquakes per year (hint: make a histogram with each year as a bin
  4. Get started so you can ask questions on Tuesday
You will build a web page for this assignment attach a link to it from your "Assignments" page. Your site should contain:
  1. Map of earthquake locations (your latitude vs. longitude plot)
  2. Histogram of number of earthquakes per year
  3. A link to your M-file (Matlab script) used to make these plots
  4. Commentary on observations using these plots. Describe any spatial or temporal patterns you see in the data and comment on what it might mean in terms of processes operating under the volcano.
Grading Rubric: Assignment is due Thursday, September 29, 2011.

GLG410 Computers in Earth and Space Exploration

Last modified: September 22, 2011