Step 1 Load Data:
Option 1: from a MATLAB binary (.mat) file
>> load SampleData
Option 2: Copy and Paste
>>newdata=[ "now paste it all here
....end with" ]
8. If you pasted in a matrix, you may need to create individual variables
Other Options: MATLAB can read data from text files and csv files. There is an "Import Data" button that can help.
Step 2 Examine your workspace and variables:
You should be aware of what data currently exist in your workspace (data that are loaded) as well as the names and sizes of any variables in your workspace. This can be done by looking in Workspace window on MATLAB, or by typing "whos" at the MATLAB prompt. In the SampleData.mat file you will see that there are three variables:
Step 3 Basic Expressions:
MATLAB can be used to organize and visualize databases, as well as do simple to complex math on these datasets.
MATLAB also has a variety of functions (list is found here), which are a series of mathematical equations that operate on your data. We will use a function called mean, which simply takes the arithmetic mean or average of a data. Let's do this for temperature first. A function works in that it provides an output and accepts input(s). In the case of mean, you need to send in the data you want to average, and what dimension you want to average over. Let's try this two ways:
>> TMEAN1 = mean(T,1);
>> TMEAN2 = mean(T,2);
Examine the sizes of the resultant variables TMEAN1 and TMEAN2. Can you figure out what the difference was between the two calls we made to function mean?
The first call took the average over dimension 1, which was years. This in a pinch computed the average temperature for each month of the year over the 1895-2014 period of record, so we end up with 12 values, one for each month.
By contrast, the second call took the average over dimension 2, which was months. This computes the average annual temperature, so we end up with a single value for each year from 1895-2014, or 120 data points.
These can also be performed on an array or matrix of data. For example, we will convert the temperature data that were loaded in with SampleData.mat from Fahrenheit to Celsius. The conversion is very simple, a change in temperate of 1 degree Celsius is 1.8 deg Fahrenheit and by definition 0 degrees Celsius is 32 degrees Fahrenheit. Initially we have our data (TMEAN1) in degrees F. We'll use a two step process to convert the data, first putting the data in a temporary variable, and then dividing this by 1.8 to get our final answer saved in variable named TinC.
>> tempvariable=TMEAN1-32;
>> TclimoC=tempvariable/1.8;
While the iron is hot let's also do this for TMEAN2
>> tempvariable=TMEAN2-32;
>> TinCyear=tempvariable/1.8;
Step 4 Basic Plotting:
MATLAB has countless ways to visualize data, see: http://www.mathworks.com/discovery/gallery.html
A picture is worth a thousand words and likewise a plot is worth at least a good 500 words. The simplest of plotting options in MATLAB is
>>plot(TclimoC)
However, this plot leave much to be desired. Let's include the years to be plotted since that is one of the variables in your workspace
>>plot(1:12,TclimoC)
Let's now add some labels to the plot so that your friend in a different class will actually be able to read it. We do this with the functions xlabel and ylabel
>>xlabel('Month')
>>ylabel('Monthly Temperature (\circC)')
We can add in the months on the x-axis as
>>set(gca,'xtick',1:12,'xticklabel','J|F|M|A|M|J|J|A|S|O|N|D');
You can do many things to improve upon the aesthetics of your figures.
Option: Using a function created my Dr. A to create a nicer looking climatology. Download the following file and put it in your working folder.
>>climograph(TclimoC,Pclimom);
4. You can add a title to your graph too:
>> title('Idaho statewide climatology 1895-2014','fontsize',24,'fontname','luxi sans')
5. You can save a copy of your figure several ways. The default option will save a figure that can ONLY be read in MATLAB, so not recommended. I like to use the command line to save my figures. Something such as the following will save a figure called myfigure.png to your current directory.
>>print(gcf,'-dpng','myfigure.png')
6. You can also use the GUI, but choose "Save As" and then select a format that you recognize as supporting graphics (.jpg, .png, .gif, .pdf).
Option 1: from a MATLAB binary (.mat) file
- Acquire data. You can download an example file at: http://nimbus.cos.uidaho.edu/hegewisch/MATLAB/SampleData.mat This contains Idaho statewide monthly temperature and precipitation from 1895-2014.
- Save this data in place you know and then navigate to that directory in MATLAB
- In MATLAB type "load [filename]"
>> load SampleData
Option 2: Copy and Paste
- In MATLAB you can paste an array and/or matrix of data from a website in
- For example go to the NCDC Climate at a Glance webpage: http://www.ncdc.noaa.gov/cag/time-series/us
- Choose 12-month average temperature ending in December for the state of Idaho
- Below the plot, where it says "Download:" click on the Excel icon and a csv file will appear on the screen
- If the above directions failed just hit this link.
- Select and copy all of the numbers starting with "1895..."
- In MATLAB add this to a new variable
>>newdata=[ "now paste it all here
....end with" ]
8. If you pasted in a matrix, you may need to create individual variables
Other Options: MATLAB can read data from text files and csv files. There is an "Import Data" button that can help.
Step 2 Examine your workspace and variables:
You should be aware of what data currently exist in your workspace (data that are loaded) as well as the names and sizes of any variables in your workspace. This can be done by looking in Workspace window on MATLAB, or by typing "whos" at the MATLAB prompt. In the SampleData.mat file you will see that there are three variables:
- T, which has the size 120 (years) x 12 (months)
- P, which has the size 120 (years) x 12 (months)
- years, which has the size 120 years
Step 3 Basic Expressions:
MATLAB can be used to organize and visualize databases, as well as do simple to complex math on these datasets.
- Addition/Subtraction: >> 2+2
- Multiplication/Division: >> 2*3
- Powers: >> 2^3
MATLAB also has a variety of functions (list is found here), which are a series of mathematical equations that operate on your data. We will use a function called mean, which simply takes the arithmetic mean or average of a data. Let's do this for temperature first. A function works in that it provides an output and accepts input(s). In the case of mean, you need to send in the data you want to average, and what dimension you want to average over. Let's try this two ways:
>> TMEAN1 = mean(T,1);
>> TMEAN2 = mean(T,2);
Examine the sizes of the resultant variables TMEAN1 and TMEAN2. Can you figure out what the difference was between the two calls we made to function mean?
The first call took the average over dimension 1, which was years. This in a pinch computed the average temperature for each month of the year over the 1895-2014 period of record, so we end up with 12 values, one for each month.
By contrast, the second call took the average over dimension 2, which was months. This computes the average annual temperature, so we end up with a single value for each year from 1895-2014, or 120 data points.
These can also be performed on an array or matrix of data. For example, we will convert the temperature data that were loaded in with SampleData.mat from Fahrenheit to Celsius. The conversion is very simple, a change in temperate of 1 degree Celsius is 1.8 deg Fahrenheit and by definition 0 degrees Celsius is 32 degrees Fahrenheit. Initially we have our data (TMEAN1) in degrees F. We'll use a two step process to convert the data, first putting the data in a temporary variable, and then dividing this by 1.8 to get our final answer saved in variable named TinC.
>> tempvariable=TMEAN1-32;
>> TclimoC=tempvariable/1.8;
While the iron is hot let's also do this for TMEAN2
>> tempvariable=TMEAN2-32;
>> TinCyear=tempvariable/1.8;
Step 4 Basic Plotting:
MATLAB has countless ways to visualize data, see: http://www.mathworks.com/discovery/gallery.html
A picture is worth a thousand words and likewise a plot is worth at least a good 500 words. The simplest of plotting options in MATLAB is
>>plot(TclimoC)
However, this plot leave much to be desired. Let's include the years to be plotted since that is one of the variables in your workspace
>>plot(1:12,TclimoC)
Let's now add some labels to the plot so that your friend in a different class will actually be able to read it. We do this with the functions xlabel and ylabel
>>xlabel('Month')
>>ylabel('Monthly Temperature (\circC)')
We can add in the months on the x-axis as
>>set(gca,'xtick',1:12,'xticklabel','J|F|M|A|M|J|J|A|S|O|N|D');
You can do many things to improve upon the aesthetics of your figures.
Option: Using a function created my Dr. A to create a nicer looking climatology. Download the following file and put it in your working folder.
- Convert your precipitation data (in inches) to millimeters. Quick lookup: 1 inch equals 25.4 mm.
- Create a monthly climatology of precipitation data for Idaho, resulting in 12 data points, one for each month. Let's call the new data you created Pclimom
- Call the function climograph by sending in input TclimoC and Pclimom
>>climograph(TclimoC,Pclimom);
4. You can add a title to your graph too:
>> title('Idaho statewide climatology 1895-2014','fontsize',24,'fontname','luxi sans')
5. You can save a copy of your figure several ways. The default option will save a figure that can ONLY be read in MATLAB, so not recommended. I like to use the command line to save my figures. Something such as the following will save a figure called myfigure.png to your current directory.
>>print(gcf,'-dpng','myfigure.png')
6. You can also use the GUI, but choose "Save As" and then select a format that you recognize as supporting graphics (.jpg, .png, .gif, .pdf).