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

Task 4.5 Detail: Writing values to a text file

Summary of new tools and commands.

Task: Use a script to create a csv file for AnomalyTemp created in task 1 for 2005 to 2010. Have 3 output columns: year, month, AnomalyTemp. Use csvread to read the file you just created to recover the values. Create a time variable from year and month and plot the anomaly temperature vs time.

There are various ways to write values to an output file in a text format that is human readable, or might be imported into other analysis software. Two are introduced here, which should satisfy most of your needs. A more comprehensive output mechanism, with much more control and flexibility will be introduced later.

A comma separated (.csv) file can be created with csvwrite('FileName.csv',DATA); where DATA is a matrix holding the values to be written. The file will be written to the current working directory.

The concatenation operators can be used to construct the data matrix in the following way, using the variables that we created in earlier tasks for this class.

DATA=[year month temp AnomalyTemp];
csvwrite('NorfolkTemp.csv',DATA);
The concatenation assumes that the variables are all column vectors with the same length. You can check that they are by using the size command, which should return some number of rows and 1 column. If one or more of the arrays is a row vector (1 row and several columns) then use the transpose operator ' to convert the row vector into a column vector.

If you want to use a different delimiter, the appropriate command is dlmwrite, which is used as follows:

DATA=[year month AnomalyTemp];
dlmwrite('NorfolkTemp.dat',DATA,' ');  % single space delimiter
dlmwrite('NorfolkTemp.dat',DATA,'\t');  % TAB delimiter
dlmwrite('NorfolkTemp.dat',DATA,',');  % comma delimiter (csv)
A restriction of this command is that only a single character is allowed as a delimiter.

Flow chart for task.

%%%  task45
%%%  continue from task 4.1
%%%  combine variables into single array variable
%%%  write data to csv file
%%%  clear variables
%%%  read data from csv file, extract variables
%%%  create time variable
%%%  plot anomaly temperature
%%%  add legends and a title

Script to complete this task:

%%%  Task 4.5
%%%  continue from task 4.1
%        either run the task 4 script or put commands here
%%%  combine variables into single array variable
 DATA=[year month temp AnomalyTemp];
%%%  write data to csv file
 csvwrite('NorfolkTemp.csv',DATA);
%%%  clear variables
 clear DATA year month temp AnomalyTemp
%%%  read data from csv file, extract variables
  data=csvread('NorfolkTemp.csv');
  year=data(:,1);month=data(:,2);AnomalyTemp=data(:,4);
  clear data
%%%  create time variable
  time=year + (month -.5)/12;
%%%  plot anomaly temperature
  figure
   plot(time,AnomalyTemp,'b')
%%%  add legends and a title
   title('Norfolk Monthly Mean Temp - mean')
   xlabel('time(yr)')
   ylabel('deg F')

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


email: J. Klinck