Read data from text file
A = fscanf(
reads
data from an open text file into column vector fileID
,formatSpec
)A
and
interprets values in the file according to the format specified by formatSpec
.
The fscanf
function reapplies the format throughout
the entire file and positions the file pointer at the end-of-file
marker. If fscanf
cannot match formatSpec
to
the data, it reads only the portion that matches and stops processing.
The text file is indicated by the file identifier, fileID
.
Use fopen
to open the file, specify the character
encoding, and obtain the fileID
value. When you
finish reading, close the file by calling fclose(fileID)
.
A = fscanf(
reads
file data into an array, fileID
,formatSpec
,sizeA
)A
, with dimensions, sizeA
,
and positions the file pointer after the last value read. fscanf
populates A
in
column order. sizeA
must be a positive integer
or have the form [m n]
, where m
and n
are
positive integers.
Format specifiers for the reading functions sscanf
and fscanf
differ
from the formats for the writing functions sprintf
and fprintf
.
The reading functions do not support a precision field. The width
field specifies a minimum for writing but a maximum for reading.
MATLAB reads characters using the encoding scheme associated
with the file. You specify the encoding when you open the file using
the fopen
function.