MATLAB Function Reference | Search  Help Desk |
sscanf | Examples See Also |
Read string under format control
A
=
sscanf(s,
format
)
A
=
sscanf(s,
format
,size)
[A,count,errmsg,nextindex]
=
sscanf(...)
A = sscanf(s,format
)
reads data from the MATLAB string variable s
, converts it according to the specified format
string, and returns it in matrix A
. format
is a string specifying the format of the data to be read. See "Remarks" for details. sscanf
is the same as fscanf
except that it reads the data from a MATLAB string variable rather than reading it from a file.
A = sscanf(s,format
,size)
reads the amount of data specified by size
and
converts it according to the specified format
string. size
is an argument that determines how much data is read. Valid options are:
If the matrix A
results from using character conversions only and size
is not of the form [M,N],
a row vector is returned.
sscanf
differs from its C language namesakes scanf()
and fscanf()
in an important respect -- it is vectorized in order to return a matrix argument. The format
string is cycled through the file until an end-of-file is reached or the amount of data specified by size
is read in.
[A,count,errmsg,nextindex]
= sscanf(...)
reads data from MATLAB string variable s
, converts it according to the specified format
string, and returns it in matrix A
. count
is an optional output argument that returns the number of elements successfully read. errmsg
is an optional output argument that returns an error message string if an error occurred or an empty matrix if an error did not occur. nextindex
is an optional output argument specifying one more than the number of characters scanned in s
.
When MATLAB reads a specified file, it attempts to match the data in the file to the format string. If a match occurs, the data is written into the matrix in column order. If a partial match occurs, only the matching data is written to the matrix, and the read operation stops.
The format
string consists of ordinary characters and/or conversion specifications. Conversion specifications indicate the type of data to be matched and involve the character %
, optional width fields, and conversion characters, organized as shown below: %
and the conversion character:%s
is used, an element read may use several MATLAB matrix elements, each holding one character. Use %c
to read space characters; the format %s
skips all white space.
Mixing character and numeric conversion specifications cause the resulting matrix to be numeric and any characters read to appear as their ASCII values, one character per MATLAB matrix element.
For more information about format strings, refer to the scanf()
and fscanf()
routines in a C language reference manual.
The statements
s = '2.7183 3.1416'; A = sscanf(s,'%f')create a two-element vector containing poor approximations to e and
pi
.
eval
Interpret strings containing MATLAB expressions
sprintf
Write formatted data to a string