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 Lecture 3: (Advanced) Variables and Functions

September 12

Instructor: Jeff Freymueller

x7286 Elvey 413B jfreymueller@alaska.edu

TA: Shanshan Li

Last Updated: August 27, 2017

Review on variables (assignments vs. referencing), Variables in Matlab, Writing your own functions in Matlab.

Reading

You should have already read Chapters 10.1 and 10.2, Chapter 11.1, 11.2, 11.4, 11.5 (3rd-4th editions, 10.1-10.3, 11.1, 11.2, 11.4, 11.5).

Lecture

Variables and Functions (pdf)

Be careful when creating structs that are supposed to contain cell-arrays! Here is an example that explains the odd behavior that I describe in the text below. Note, that this is well documented Matlab behavior and therefore not a bug, but a feature. Whenever someone says "that's a feature, not a bug," they mean that the program behaves in an irritating and confusing way (to the users), but that it is consistent with the program's documentation.

The problem is with initializing fields of structs that hold cell arrays when defining the struct. If you define and initialize a struct and one of the field values is a cell array, Matlab will create an array of structs, each holding one value of the cell array as value of the respective field. If you want to have a cell array as value of one of your fields, you have to hand it to the struct as a single cell. I find this behavior very irritating and unintuitive. But Mathworks says:

``Note: When calling the struct function, use one set of curly braces {} to pass multiple field values, and use two sets of curly braces {{}} to create a cell array in the specified field.''

BECAUSE: ``The MATLAB curly braces {} operator constructs a cell array. One use of cell arrays is as a convenient way to pass arguments when calling a function. This is exactly how they are used with the struct function. When you use this operator to pass multiple field values to the struct function, you are actually passing these values packaged in a cell array. The struct function, upon receiving the cell array argument, removes the field values from the cell array and assigns them to the fields specified in your struct command.'' (from http://www.mathworks.com/help/techdoc/matlab_prog/br04bw6-38.html)

Keep this in mind whenever you're stuffing a cell array into a struct getting an extra layer of data organization you didn't ask for.

LAB 03

Structs, Cells, and Functions

Examples

Examples are given in the lecture slides. If you Copy'n'Paste, be sure to replace the angled quote marks PP uses with actual single ticks!

Exercise: (Due Tuesday Sep 19)

Think about which parts of the flow chart you turned in a few weeks ago can be implemented as functions and come up with necessary input parameters and return values. It might be good idea to do this in form of a table, example:

function definitioninput value(s)return value(s)
[str, errmsg] = sprintf(format, A, ...) format: String in single quotation marks that describes the format of the output fields.
A: numeric or character array.
returns results to string str and an error message string when the operation is unsuccessful. Otherwise, errmsg is empty.


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