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 3: Variables and Functions

September 12, 14

Instructor: Jeff Freymueller

x7286 Elvey 413B jfreymueller@alaska.edu

TA: Shanshan Li

Last Updated: September 12, 2017

Due: Tuesday Sep 19, before class

Lab slides

Structures (pdf)

Introduction

During the lecture you learned about more sophisticated ways of organizing data in your Matlab scripts. With this lab you will get a little bit of practice in using such structs and cell arrays. You will also write a function. After this lab you should be in equipped with the very basics of programming so that we can move on to flow control in the coming lectures.

Exercise 0: Warm up.

Get the data, examples, stuff and unpack it.

Exercise 1: Structs and Cells

Open the file structs.m and follow us along as we explain what's happening. You're welcome read the comments again, and again, and again.

Once you've understood what's going on, take a look at timeseries_lab.m. We provide you with GPS data for the three sites FAIR, WHIT, and BZ09 - namely the latitude, longitude, and height solutions for certain GPS epochs that are defined in the respective *_time arrays. These data are stored in the file gps_data.mat which is loaded at the beginning of the timeseries_lab.m script. Load is a handy way to preserve the state of a Matlab Workspace.

You task is it to organize the data better: put the data for each station in a struct, and then create a cell array to hold the structs for all of the stations. You can add more fields to your structs if you want to. Once you've done that, add the following line below those populating the structs:

	clear BZ09_time BZ09_lon BZ09_lat BZ09_height ...
	      FAIR_time FAIR_lon FAIR_lat FAIR_height ... 
	      WHIT_time WHIT_lon WHIT_lat WHIT_height
	

This deletes the arrays previously loaded; but have no fear! You already copied everything into structs, right? Now adjust the plotting routines accordingly. If you're really motivated, you can optimize the plotting by putting everything into a for loop to iterate over your array of structs (see structs.m). You may have to introduce additional cell arrays for station names and plate names, and include them using sprintf for the title strings (or just delete them for now).

Exercise 2: Write a Matlab function!

Now it is time for you to write your own function and the code to call it. One concept that some past students had a hard time with is the distinction between the function from the code that calls it. A function is defined in its own separate m-file. That file will contain the function definition and nothing else. In a separate file, or on the command line, you will put the code that calls the function and makes use of the return values.

Here's the code of a simple example function: cuberoot.m (you got this in your examples):

	function out = cuberoot(in);
	    out = in.^(1/3);
	return	
	

Now, try it:

cuberoot(9)
x = [ 1 3 28 17 33 27 ];
y = cuberoot(x)
y = y.^3
x == y
x == int16(y)

Kinda like magic. Note the conversion from floating point to integer values at the end and the changes in results of the comparisons (Matlab is weakly typed).

Now it's up to you to write a function that is similar to cuberoot. Take a simple mathematical operation or function and implement it following the given example. Make sure you allow for both operation on scalars and vectors. Then, write a script in which you call the function using (surprise) a scalar and an array. Note that you can pass more parameters if you or your desired function need that. Turn in the m-file for your function and the m-file with the script that calls your function.

Exercise 3: Write a Matlab function to get user input

Write a second function that doesn't do computation or math. This one will prompt the user for some input, and return the values. You can use the built-in MATLAB function input to get this (see doc input for help). If you are adventurous, you can try the graphical interface function inputdlg, which puts up a dialog box that can have multiple fields (see doc inputdlg for help). But be sure to use the simpler way first.

Here is an example of using input:

foo = input('Give me some input: ', 's')

The first argument to input is the prompt that the user will see, and the second argument tells MATLAB that it should interpret the answer as a string (otherwise it assumes it is a number, and it will give an error if you enter a string). Try it out on the command window, both with and without the second argument.

For this assignment, write a function that prompts the user with two questions of your own choosing, and returns the answers as strings. Turn in the m-file for your function and an m-file for the code that calls the function.


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