MATLAB Function Reference |
Read data from text file, write to multiple outputs
Graphical Interface
As an alternative to textread
, use the Import Wizard. To activate the Import Wizard, select Import Data from the File menu.
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. textread
handles both fixed and free format files.
Note
When reading large text files, reading from a specific point in a file, or reading file data into a cell array rather than multiple outputs, you might prefer to use the textscan function.
|
textread
matches and converts groups of characters from the input. Each input field is defined as a string of non-white-space characters that extends to the next white-space or delimiter character, or to the maximum field width. Repeated delimiter characters are significant, while repeated white-space 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
routine. Values for the format
string are listed in the table below. White-space 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.
Note
When textread reads a consecutive series of whitespace values, it treats them as one white space. When it reads a consecutive series of delimiter values, it treats each as a separate delimiter.
|
Remarks
If you want to preserve leading and trailing spaces in a string, use the whitespace
parameter as shown here:
Example 1 -- Read All Fields in Free Format File Using %
The first line of mydata.dat
is
Read the first line of the file as a free format file using the %
format.
Example 2 -- Read as Fixed Format File, Ignoring the Floating Point Value
The first line of mydata.dat
is
Read the first line of the file as a fixed format file, ignoring the floating-point value.
%*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 of mydata.dat
is
Read the first line of the file, ignoring the characters Type
in the second field.
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 -- Specify Value to Fill Empty Cells
For files with empty cells, use the emptyvalue
parameter. Suppose the file data.csv
contains:
Read the file using NaN
to fill any empty cells:
Example 5 -- Read M-File into a Cell Array of Strings
Read the file fft.m
into cell array of strings.
See Also
textscan
, dlmread
, csvread
, fscanf
Text Properties | textscan |
© 1994-2005 The MathWorks, Inc.