MATLAB Function Reference |
Read string under format control
Syntax
Description
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 the 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
.
Remarks
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:
Add one or more of these characters between the %
and the conversion character.
Valid conversion characters are as shown.
If %s
is used, an element read might use several MATLAB matrix elements, each holding one character. Use %c
to read space characters, or %s
to skip 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.
Example 1
create a two-element vector containing poor approximations to e
and pi
.
Create matrix A
with both character and numeric data:
Read A
into 2-by-N matrix B
, ignoring the character data. As stated in the Description section, sscanf
fills matrix B
in column order:
If you want sscanf to return the numeric data in B in the same order as in A, you can use this technique:
See Also
ss2tf | stairs |
© 1994-2005 The MathWorks, Inc.