Read formatted data from text file or string
reads data from an open text file into a cell array, C
= textscan(fileID
,formatSpec
)C
.
The text file is indicated by the file identifier, fileID
.
Use fopen
to open the file and obtain the fileID
value.
When you finish reading from a file, close the file by calling fclose(fileID)
.
textscan
attempts to match the data in the
file to the conversion specifier in formatSpec
.
The textscan
function reapplies formatSpec
throughout
the entire file and stops when it cannot match formatSpec
to
the data.
reads
file data using the C
= textscan(fileID
,formatSpec
,N
)formatSpec
N
times,
where N
is a positive integer. To read additional
data from the file after N
cycles, call textscan
again
using the original fileID
. If you resume a text
scan of a file by calling textscan
with the same
file identifier (fileID
), then textscan
automatically
resumes reading at the point where it terminated the last read.
reads
the text from character vector C
= textscan(chr
,formatSpec
)chr
into cell array C
.
When reading text from a character vector, repeated calls to textscan
restart
the scan from the beginning each time. To restart a scan from the
last position, request a position
output.
textscan
attempts to match the data in character
vector chr
to the format specified in formatSpec
.
uses
the C
= textscan(chr
,formatSpec
,N
)formatSpec
N
times, where N
is
a positive integer.
specifies
options using one or more C
= textscan(___,Name,Value
)Name,Value
pair arguments,
in addition to any of the input arguments in the previous syntaxes.
[
returns the position in the
file or the character vector at the end of the scan as the second
output argument. For a file, this is the value that C
,position
]
= textscan(___)ftell(fileID)
would
return after calling textscan
. For a character
vector, position
indicates how many characters
textscan
read.
textscan
converts numeric fields to the specified
output type according to MATLAB rules regarding overflow, truncation,
and the use of NaN
, Inf
, and -Inf
.
For example, MATLAB represents an integer NaN
as
zero. If textscan
finds an empty field associated
with an integer format specifier (such as %d
or %u
),
it returns the empty value as zero and not NaN
.
When matching data to a text conversion specifier, textscan
reads
until it finds a delimiter or an end-of-line character. When matching
data to a numeric conversion specifier, textscan
reads
until it finds a nonnumeric character. When textscan
can
no longer match the data to a particular conversion specifier, it
attempts to match the data to the next conversion specifier in the formatSpec
.
Sign (+
or -
), exponent characters,
and decimal points are considered numeric characters.
Sign | Digits | Decimal Point | Digits | Exponent Character | Sign | Digits |
---|---|---|---|---|---|---|
Read one sign character if it exists. | Read one or more digits. | Read one decimal point if it exists. | If there is a decimal point, read one or more digits that immediately follow it. | Read one exponent character if it exists. | If there is an exponent character, read one sign character. | If there is an exponent character, read one or more digits that follow it. |
textscan
imports any complex number as a
whole into a complex numeric field, converting the real and imaginary
parts to the specified numeric type (such as %d
or %f
).
Valid forms for a complex number are:
±<real> ±<imag>i|j | Example: |
±<imag>i|j | Example: |
Do not include embedded white space in a complex number. textscan
interprets
embedded white space as a field delimiter.