Simple looping and matrix construction in Matlab

Introduction

This lecture is our fist introduction to what is called "control flow" in programming. We will start out with For Loops to demonstrate how you can do many repetative tasks without explicitly writing each successive iteration.

For loops

Help on for loops can be found here.
Sometimes, you need go go through a data set to do some analysis and you cannot do it in the simple vector mode of Matlab.
For example,

In this example, I have searched for the depth values within each step (+/- half the step size from the lat long location of the estimate). Then, I computed their mean, converted the mean and some text to a string (sprintf), and plotted that amount using text.

3D Plots of Locations - plot3

We could also plot the locations in 3D using the earthquake depth column. Use the plot3 function:

NOTE: The colorbar function will plot the color scale for the image. The set function is used to change Properties after you have plotted your data. Here we change the property, Ydir to plot the y-axis so that larger values are above smaller values. The first argument in set is gca, which tells the set command:"I want to set a property in the current axis (the axis you just plotted your data in)."

We can then plot the earthquakes over the image, along with the mean depth values in each box:

pcolor is similar to imagesc

If you notice the above figure plots grids where there is "no data" (NaN) as blue. This is a limitation of imagesc. The function pcolor plots a pseudocolor image of your matrix. This is very similar to a scaled image produced by imagesc. If you use this function, your "no data" blocks will now be white.

FIRST, we need to precondition our x-axis vector (long_range), y-axis vector (lat_range), and matrix (mean_depth). This is because pcolor plots the colors at the vertices (not the center of the grid like imagesc).