Textread

Textread is a function which handles many of the problems/tasks of file input for you. The syntax for textread is : "array_variable = textread( file_name );". File name is a variable containing a string. Array_variable is a variable which will "receive" the contents of the file.

Textread

Matlab has a powerful built in function which will read information out of a file and store it in an array. This function is called textread.

	
	your_array_variable = textread('name_of_file');
	
      

For example to read a list of grades from a file called 'list_of_grades', you would simply write:

	
	
	file_name  = 'list_of_grades';
	
	grades = textread( file_name );
	
      

The variable grades will be an array of numbers, one for each grade!

textread is a function that "takes in" the name of a file and "returns" an array of information.

The file should contain a column of data. (Note: textread can also read files with multiple columns of data).

Advanced Syntax

Textread takes several optional parameters which can be used to influence how it works. Below we discuss various situations you might encounter.

Textread is designed to read a list of numbers, one per line. Textread will also happily read a 'matrix' of numbers. Consider the following data file (named 'data' in the current folder):

	1 2 3 4
	5 6 7 8
	9 10 11 12
      
	
	>> textread('data')
	ans =
	   1  2  3  4
	   5  6  7  8
	   9 10 11 12
	

      

Above, textread reads the data as a Matrix (3x4 array of doubles).

	
>> [col1, col2, col3, col4] = textread('data')

col1 =

     1
     5
     9


col2 =

     2
     6
    10


col3 =

     3
     7
    11


col4 =

     4
     8
    12
	
      

Above, textread reads the data as four columns into four separate variable.

Now assume the data had a header ("comment") line at the top of the file:

	% This is a comment
	1 2 3 4
	5 6 7 8
	9 10 11 12
      

'headerlines' parameter

To tell textread to skip the first line of the file (the non-data line), we use the optional arg "'headerlines', number" where number is how many lines to skip.

		
	>> [col1, col2, col3, col4] = textread('data', 'headerlines', 1);
	
      

Strings and Things

Suppose you wanted to read a file with strings in it as well as numbers. To do this, you must specify the "%format" of each column of data. For example:

	% This is a grade file
	jim 99 87 98
	jess 94 92 91
	jenna 100 90 95
      

To read this file we use:

		
	>> [names, grade1, grade2, grade3] = textread('data', '%s %f %f %f', 'headerlines', 1);
	
      

All it can get

It should be noted that textread attempts to read all the information it can from a file. Its a "one shot gets all" type of function.

Reading a single value

To read a single value from a file you would modify textread by passing in another parameter saying how many values to read.

		
	textread('file_name', '%d', 1); %  This reads a single integer from the file.
	
      

To read 100 values, we could either use this code 100 times using a loop, or use the value 100 in textread.

		
	textread('file_name', '%d', 100); %  This reads a 100 integers from the file.

	for i=1:100  % this calls the textread function 100 times
	  textread('file_name', '%d', 1); %  This reads a single integer from the file.
	  % each subsequent call to textread reads the NEXT value from the file
	  % thus we are NOT reading the first value over and over again.
	end
	
      


Back to Topics List