Matlab Class Home      Class Outline      Previous Task      Next Task      Main Class Page      Evaluation 4

Task 4.1 Detail: Read values from a file containing columns of numbers; calculate mean.

Summary of new tools and commands.

Task: Read the air temperatures from Norfolk airport (details below). Construct a time variable (units of years) based on year and month (being 1/12 of a year). Calculate the mean temperature over the whole record. Plot the temperature vs time. Add a line to the plot indicating the mean temperature. Add appropriate labels and a title.

The data file NorfolkMeanTemp2005.dat contains 3 columns (separated by spaces): year (2005-2010), month (1-12) and the mean temperature at Norfolk Airport (ORF) for that month in degrees Fahrenheit. Save the file to your current working directory. You may give the file any name that you want; its default name is "NorfolkMeanTemp2005.dat". To save the file, go to the "file" pulldown menu on your browser and choose "save page as". A dialog menu should appear allowing you to choose a folder and file name.

We will start reading numbers with relatively simple tools. The simplest file format is a file containing columns of numbers separated by one or more spaces. There can be only numbers in the file. Every column must have the same number of entries (no missing or blank values).

The basic command to use is data=load('FILENAME'); where FILENAME is the exact name of the file. The values will be in a matrix called data, for this example, but it can be any name that you want.

Assume that the data file, as in the task above, has three columns which might be year, month and temperature. It is convenient to extract the columns from the input and call them something more descriptive as is done in the following commands.

data=load('TempData.dat');
year=data(:,1);
month=data(:,2);
tempF=data(:,3);
The syntax data(:,2) means "all rows", indicated by the use of : in the row index, for the second column. You can now work with the variables year, month and tempF rather than data(:,1), data(:,2), and data(:,3).

We may not know ahead of time how many values there are in the file. Or we might want to work with a number of files each with different numbers of values. We can determine the number of values with the command ntime=length(year), where ntime is my choice for the variable name.

It is useful to know the mean of a set of numbers. Use the command sum to calculate the sum of the values in a vector. Then the mean is calculated by

  MeanTemp=sum(tempF)/length(tempF);
  AnomalyTemp=tempF-MeanTemp;
The anomaly temperature is defined as the departure of the temperature from the mean. Note that temp is a vector and MeanTemp is a scalar (a single number). So MATLAB subtracts the single number from each value of the vector, creating another vector (AnomalyTemp) without special instructions (explicit looping instructions) to do so.

The command length returns the total number of values in a vector or matrix. It is often necessary to know the number of rows and columns in a variable, which is obtained by the command
[nr nc]=size(VARIABLE);, where nr and nc are the number of rows and number of columns, respectively.

What does the sum command do if the variable is a matrix instead of a vector? By default sum, and most other MATLAB commands, works on columns of multidimensional variables. So if M is a matrix with 3 columns, then S=sum(M) will return 3 values, each being the sum of the values in each of the columns.

If you want the sum of all values in the variable, then use TotalSum=sum(sum(M));. The inner sum returns the sum over columns of M, while the outer sum adds the row sums across the columns. A newer option is TotalSum=sum(M,'all');, which sums across all dimensions of a multi-dimension array.

You can control the dimension over which the sum is done by adding an option to the command: S=sum(M,2);, where the second argument is the dimension over which to make the sum. In this case, it is over the second dimension (the columns). So it will sum across rows in the table.

The flow chart for the task:

%%%  Task 4.1
%%%  read data file and rename columns
%%%  create a time variable
%%%  calculate the mean
%%%  plot temperature
%%%  add labels and title
%%%  add line for mean

Script to complete this task:

%%%  Task 4.1
%%%  read data file and rename columns
  data=load('NorfolkMeanTemp2005.dat');
  year=data(:,1);month=data(:,2);temp=data(:,3);
%%%  create a time variable
  time=year + (month -.5)/12; % middle of a month
  clear data
%%%  calculate the mean
  MeanTemp=sum(temp)/length(temp);
%%%  plot temperature
  figure
   plot(time,temp,'b')
%%%  add labels and title
   title('Norfolk Monthly Mean Temp')
   xlabel('time(yr)')
   ylabel('deg F')
%%%  add line for mean
   hold on
   plot([time(1) time(end)],[MeanTemp MeanTemp],'r','LineWidth',2)
   hold off

Matlab Class Home      Class Outline      Previous Task      Next Task      Main Class Page      Evaluation 4


email: J. Klinck