 # GLG410 Introduction to MatLab

We will use MATLAB for the rest of our class in conjunction with notes from various sources and our textbook Middleton, 1999, Data analysis in the earth sciences using Matlab, Prentice Hall.

## Demos

`help matlab demos`
Census
Quake
graf2d
graf2d2
penny
earthmap
fifteen
xpbombs
crulspin
wrldtrv

## Using the matlab help function

```>> help

HELP topics:

matlab/general       -  General purpose commands.
matlab/ops           -  Operators and special characters.
matlab/lang          -  Programming language constructs.
matlab/elmat         -  Elementary matrices and matrix manipulation.
matlab/elfun         -  Elementary math functions.
matlab/specfun       -  Specialized math functions.
matlab/matfun        -  Matrix functions - numerical linear algebra.
.
.
.
toolbox/symbolic     -  Symbolic Math Toolbox.
toolbox/tour         -  MATLAB Tour
wavelet/wavelet      -  Wavelet Toolbox.
wavelet/wavedemo     -  Wavelet Toolbox Demos.

For more help on directory/topic, type "help topic".
```

## Basic math function

Say we wanted to figure out some basic math function:
```>> help matlab/elmat
```
and then in that output, we wanted to know how to create random numbers:
```>>help rand
```

So let's make a simple coin flipper:

```>> round(rand(1))

ans =

1

>> round(rand(1))

ans =

0

>> round(rand(1))

ans =

1
```

or, increase your chances, flip 4 at once:

```>> round(rand(2))

ans =

0     1
1     0

>> round(rand(2))

ans =

0     0
1     1
```

What about clearing variables? CLEAR removes all variables from the workspace.

## Matlab resources

The Mathworks

Download and view Professor Rankin's Matlab Guide:
http://www.eas.asu.edu/~rankin/resources/MatLabRev.pdf once you save it to your directory, you will need to type: `darkwing% acroread &`
that will launch the Adobe Acrobat Reader and then you can open the Matlab guide

Using Matlab For Geological Applications
Working our way through that will be our main effort for our class and lab today.

## Tidbits

### Saving figures

Once you have made a graphic you like in MatLab, go to the file menu on the graphics window and print it to a file: Save the file to a name and location so you can find it. The file is a postscript file so you can do a number of things to it including
`darkwing% gs figure.ps`
A NEW function we have is the convert command. It is pretty smart and it will easily convert the *.ps to *.gif. NOTE that it only makes a gif file. You have to use ghostscript to make a jpeg:
`darkwing% convert figure.ps figure.gif`
What is important is that the suffixes (*.ps and *.gif) are exactly as shown because that is where convert goes to look and see what you want.

If you want to load a file that has some data in it, you thave to be in the right directory or type the complete path name:
`>> load /export/home2/ramon/glg410matlab/middleton_files/DEWIJS.DAT`
to display the contents:
>> DEWIJS

If you produce your own text file for matlab, ftp it onto darkwing (if you did not produce it there), and then you can do the same to a file with one entry per line:
```darkwing% more dates
1993.93584
1993.93858
1993.94132
1993.94406
1993.9468
1993.94954
1993.95228
1993.95502
1993.95776

.
.
.
```
```>> load dates
>>dates
1.9939
1.9939
1.9939
1.9939
.
.
.
```
You will notice that the formatting is bad. The numbers look like they have changed. Try `help format`.
then you will see that if you type:
```>>format long g
>>dates
1993.93584
1993.93858
1993.94132
1993.94406
.
.
.
```

### Using the diary function to record your Matlab session

See section R.1.6 from Professor Rankin's Matlab Guide.

Pages maintained by
Prof. Ramón Arrowsmith