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

Task 4.3 Detail: Read numbers from excel (.csv) files.

Summary of new tools and commands.

Task: Read the Norfolk airport temperatures from the csv file (details below). Construct a running 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.

A sample csv data file NorfolkMeanTemp1990.csv contains 3 columns (separated by commas): year (1990-1995), month (1-12) and the monthly mean temperature at Norfolk Airport (ORF) in degrees Fahrenheit.

Many times, data values are provided in Excel files. There are several ways to import this information into MATLAB, but the simplest is to use Excel to save the numbers from the Excel file into a simpler, comma separated (.csv), format. This is a text format which can be read, and modified by simple text editors.

The important MATLAB requirement for the file is that the columns be complete; that every row in the file has the same number of columns. If there are missing values, indicated by a field containing only blanks, then MATLAB will convert that value to zero.

In addition, there can only be numbers in the file.

The following command will read a csv file:

  data = csvread('FILE.csv');
where a matrix is created having the same number of rows and columns as the csv file. The various columns can be saved under different variable names, as is done in the first task for this class.

If the file contains a header line, then it can be ignored with

  R=1;C=0;
  data = csvread('FILE.csv',R,C);
where the two added numbers tell csvread which row and column to begin reading. Note that these counters start at zero, so R=1 (above) says to start reading on the second row.

If there is a leading column in the file (say, containing a date string), then it can be ignored by setting C=1. Unfortunately, there is no mechanism to ignore an interior row or column.

A more general delimited file reader is available: dlmread. It allows different delimiters between the columns. It has the ability to detect the delimiter characters from the file structure. The first task in this class read a file with columns separated by one or more blanks. The following command will read that file, similar to the action of load.

  data = dlmread('NorfolkMeanTemp2005.dat');
It can also read a comma separated file:
  data = dlmread('NorfolkMeanTemp1990.csv');
In both of these cases, no delimeter is given, so MATLAB analyzes the structure of the file to determine what the delimeter is.

You can specify the delimeter if you want with

  data = dlmread('NorfolkMeanTemp1990.csv',',');
where the second argument is a character string identifying the delimiter. Finally, it is possible to skip header lines and columns with approprate values for R and C,
  data = dlmread('NorfolkMeanTemp1990.csv',',',R,C);

Some care is needed with files delimited by one or more spaces. If you specify a delimiter of a single space, then multiple spaces between numbers can cause problems reading a file. What happens is that dlmread interprets two spaces in the same way that Excel interprets two commas, assuming there is a blank value between the two spaces. This creates columns of zeros in places were entries are separated by two blanks.

One solution to this problem is to edit the data file and replace two blanks with a single blank (use the find/replace command; don't do this by hand). You might need to do this several times to reduce all multiples spaces to only single spaces. The other solution (above) is to not specify the delimiter and let MATLAB deal with the problem of multiple spaces.

Flow chart for task

%%%  Task 43
%%%  read data file, extract variables
%%%  create time variable
%%%  calculate the mean temperature
%%%  plot the temperature
%%%  add labels and title
%%%  add the mean line

Script to complete this task:

%%%  Task 43
%%%  read data file, extract variables
  data=csvread('NorfolkMeanTemp1990.csv');
  year=data(:,1);month=data(:,2);temp=data(:,3);
%%%  create time variable
  time=year + (month -.5)/12;
  clear data
%%%  calculate the mean temperature
  MeanTemp=mean(temp);
%%%  plot the temperature
  figure
   plot(time,temp,'b')
%%%  add labels and title
   title('Norfolk Monthly Mean Temp')
   xlabel('time(yr)')
   ylabel('deg F')
%%%  add the mean line
   hold on
   plot([time(1) time(end)],[MeanTemp MeanTemp],'r')
   hold off

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


email: J. Klinck