Home
Research Projects
Classes GEOS 293/393 Hawaii Field Course GEOS 436/636 Beyond the Mouse GEOS 602 Geophysical Fields GEOS 620 Geodynamics GEOS 655 Tectonic Geodesy GEOS 692 Geophys. Seminar
Current & Past Students
Student Opportunities
Curriculum Vitae
Recent Publications
Info for Surveyors
Personal Info

Climb higher, beyond the mouse...


Beyond the Mouse LAB 4: Control Structures

September 19, 21

Instructor: Jeff Freymueller

x7286 Elvey 413B jfreymueller@alaska.edu

TA: Shanshan Li

Last Updated: September 12, 2016

Due: Tuesday September 26, before class

Lab slides

NONE.

Introduction

In this week's lecture we introduced constructs that allow you to increase the complexity of problems you can solve with your programs. Flow control language features like if-else, for, or while allow you to create tree like structures of code rather than one long list of function calls. Great! Today you'll expand the problem we worked on last week - the GPS time series. The goal is to have you create a Matlab script which will process a bunch of data files: load data, put it in an array of structs, create figures.

Exercise

You will use the data from last week. Download this archive (zip) to get the data files and necessary functions. This time we are going to do the exercise in a somehwat more sophisticated, and much more generalized, way. Some of you might have gotten partway to here in your solution to last week's exercise. You should be able to re-use some of the code from last week, but think about what changes before you just copy and paste.

Let me write out in detail what I expect you to do:

Now add some better error checking: textread will complain (throw an error) if a file does not exist. Given that you might be dealing with hundreds of stations at a time it would be frustrating to stop possibly lenghty processing whenever there is a typo in the list of site names, or if certain files are just not there. Guess what: try-catch will help!

Try putting the read_gps_data function call in a try-block and catch possible errors. Give a useful error message (see the lecture notes) OR display your very own error message, i.e. file X does not exist, where X is the parameter you handed to read_gps_data. Then add a bogus station name to the cell array that holds the station names and run your script. You should get at least one empty plot and a meaningful message, but the script should not abort - yeah!

Are you done already? Extra credit

Take a look at the code below, which uses the dir function to get a list of the files matching the name wildcard *.dat.
listing = dir('*.dat');
nfiles = length(listing);
listing(3).name
strrep(listing(3).name, '.dat', '')

Make another version of your script that gets a list of the available data files and plots each one, instead of depending on you to make up the list of names. You will either want to put each name into an element of the cell array, or else change your code to use the struct array listing to provide the station names. You can get the station name from the file name by using the function strrep to replace the string '.dat' with '' (an empty string).

Does this example show you why it is really useful to use a systematic scheme for naming files?

Helpful Matlab Functions:

Here is a list of functions that might be of help.


Dr. Jeffrey T. Freymueller
Professor of Geophysics
Geophysical Institute
University of Alaska, Fairbanks
Fairbanks, AK 99775-7320

jfreymueller -at- alaska.edu
Phone 907-474-7286
Fax 907-474-7290
Office 413B Elvey