University of Alaska Fairbanks
Geophysical Institute

Beyond the Mouse 2010 - The geoscientist's computational chest.

Lab 3: Matlab - Structs and Functions

"Programming is legitimate and necessary academic endeavor."
Donald E. Knuth

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 I: Structs and Cells (Commented Solution)

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. You're very welcome to ignore the parts that are bracketed with SIDE TRACK.

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. This is a 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 maybe all stations in an array of structs (make sure all structs have the same fields for that!). 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 II: Write a Matlab function! (no solution given)

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.

ronni <at> gi <dot> alaska <dot> edu | Last modified: February 01 2013 21:41.