| MATLAB Function Reference | Search  Help Desk |
| textread | Examples See Also |
Read formatted data from text file
Syntax
[A,B,C,...] = textread('filename','format')
[A,B,C,...] = textread('filename','format',N)
[...] = textread(...,'param','value',...)
Description
[A,B,C,...] = textread('filename','format')
reads data from the file 'filename' into the variables A,B,C, and so on, using the specified format, until the entire file is read. textread is useful for reading text files with a known format. Both fixed and free format files can be handled.
textread matches and converts groups of characters from the input. Each input field is defined as a string of non-whitespace characters that extends to the next whitespace or delimiter character, or to the maximum field width. Repeated delimiter characters are significant, while repeated whitespace characters are treated as one.
The format string determines the number and types of return arguments. The number of return arguments is the number of items in the format string. The format string supports a subset of the conversion specifiers and conventions of the C language FSCANF function. Values for the format string are listed in the table below. Whitespace characters in the format string are ignored.[A,B,C,...] = textread('filename','format',N)
reads the data, reusing the format string N times, where N is an integer greater than zero. If N is smaller than zero, textread reads the entire file.
[...] = textread(...,'param','value',...)
customizes textread using param/value pairs, as listed in the table below.Examples
Example 1 - Read All Fields in Free Format File Using %
The first line ofmydata.dat is
Sally Type1 12.34 45 YesRead the first line of the file as a free format file using the
% format.
[names,types,x,y,answer] = textread('mydata.dat','%s %s %f ...
%d %s',1)
returns
names =
'Sally'
types =
'Type1'
x =
12.34000000000000
y =
45
answer =
'Yes'
Example 2 - Read as Fixed Format File, Ignoring the Floating Point Value
The first line ofmydata.dat is
Sally Type1 12.34 45 YesRead the first line of the file as a fixed format file, ignoring the floating point value.
[names,types,y,answer] = textread('mydata.dat','%9c %5s %*f ...
...
%2d %3s',1)
returns
names =
Sally
types =
'Type1'
y =
45
answer =
'Yes'
%*f in the format string causes textread to ignore the floating point value, in this case, 12.34.
Example 3 - Read Using Literal to Ignore Matching Characters
The first line ofmydata.dat is
Sally Type1 12.34 45 YesRead the first line of the file, ignoring the characters
Type in the second field.
[names,typenum,x,y,answer] = textread('mydata.dat','%s Type%d %f
%d %s',1)
returns
names =
'Sally'
typenum =
1
x =
12.34000000000000
y =
45
answer =
'Yes'
Type%d in the format string causes the characters Type in the second field to be ignored, while the rest of the second field is read as a signed integer, in this case, 1.
Example 4 - Read M-file into a Cell Array of Strings
Read the filefft.m into cell array of strings.
file = textread('fft.m','%s','delimiter','\n','whitespace','');
See Also
dlmread, sscanf