# Working with NOAA stream gauge data in MATLAB

Initial Publication Date: September 18, 2020 | Reviewed: April 13, 2021

## Summary

Students work through the steps of finding freely available time series stream gauge data, importing a plain text file into MATLAB, then manipulating data and plotting it in MATLAB. They practice file input/output with .txt and .mat files, working with date & time commands in MATLAB, and they practice writing .m files in the activity.

Used this activity? Share your experiences and modifications

## Learning Goals

The skills covered here are:

1. Use the Import Wizard to read in a plain text file
2. Use MATLAB to manipulate time series data
3. Write out a .mat file
4. Write a .m script that uses the .mat file, and makes a nice plot

I also use this lesson to try to teach the habit of mind of workflow. What I mean by this is that students need help learning to be good scientific collaborators by logging the source of their data and logging what operations they performed on it so another person (including their future selves) can figure out and/or recreate what they did.

MATLAB is used so students continue to learn to stop using non-mathematically-based spreadsheet programs to analyze and plot their data.

## Context for Use

I teach this activity in an undergrad geosciences course at a 4-year college. Most of the students are geoscience majors, some come from other science or engineering departments. When I teach this activity, it takes one classroom period to work through. At the beginning of the semester students know zero MATLAB. This activity appears about one third of the way through, so students know how to write a .m script, they know how to make and label a plot, they know basic arithmetic and how to construct and to logically query a vector. They have had one class period to introduce them to date and time commands in MATLAB. [This intro to the date and time commands is pretty necessary]
I find that students have a hard time working with dates and times, but if your field has a lot of time series data, you have to do it. This activity practices that on some fairly straightforward data.

You can work through the activity just as it is. When I teach I prefer to use near-real-time data if I can, and anyone who feels the same way can easily modify this activity by going and getting the data for different stream gauges. Then, the only necessary changes will be the actual names of the files and using current dates and times instead of the ones in my scripts.

## Description and Teaching Materials

The .mlx file walks you through the activity. There are three additional external files (two plain text and one .mat) that should be put in the same directory as the .mlx file so the .mlx file can "see" them and use them in its internal code parts.
Here is a summary of the in-class activity:

1. Navigate in a web browser to NOAA's stream gauge page and take note of all the real time data stream gauges out there. Then download one you like as a plain text file. **To run my activity you don't actually have to do this, you can just use my file. This saves time and prevents you dealing with students and their different browsers, or if you want to work offline.
2. Use Import Wizard to read the file into MATLAB
3. Use date and time commands to manipulate the data into a more tractable form
4. Write newly-manipulated data out as a .mat file
5. Write a script .m file that reads in the .mat file and makes a nice plot out of the data
6. Use vector-querying skills to answer a few simple questions about the data

The homework problem asks students to repeat this activity (Repetition is the Mother of Learning! as my high school Latin teacher always used to say) for a different stream gauge. My file for that is included here if you want to use it as is.

MATLAB notebook file for the stream gauge activity (MATLAB Live Script 70kB Sep11 20)
Stream Gauge plain text data file for stream gauge activity (Text File 9kB Sep11 20)
MATLAB .mat file of the stream gauge data for the stream gauge activity (Matlab .MAT File 3kB Sep11 20)
plain text stream gauge data file for homework activity (Text File 16kB Sep11 20)

## Teaching Notes and Tips

The hardest part of this assignment is that it takes students a bit of practice to work with date and time commands. An introduction / lookup sheet with pertinent commands would be good to do before this activity. Prepare to explain how to manipulate dates and times more than once anyway.

## Assessment

I grade the homework. What I look for is being able to run their script and that it makes an adequate-looking plot (correct labels, symbols that are discernable, the correct values on the correct axes) and I'll know if they were successful when they answer the questions correctly.
I always let students work in class and ask me for help, so frequently by the time they turn things in, they work. Not always, though!!
**The most typical problem for this assignment occurs when students forget to turn in the .mat file they made along with their script, meaning I can't run their script because the file to load is missing.