MATLAB Function Reference Previous page   Next Page
strread

Read formatted data from string

Syntax

Description

A = strread('str') reads numeric data from input string str into a 1-by-N vector A, where N equals the number of whitespace-separated numbers in str. Use this form only with strings containing numeric data. See Example 1 below.

[A, B, ...] = strread('str') reads numeric data from the string input str into scalar output variables A, B, and so on. The number of output variables must equal the number of whitespace-separated numbers in str. Use this form only with strings containing numeric data. See Example 2 below.

[A, B, ...] = strread('str', 'format') reads data from str into variables A, B, and so on using the specified format. The number of output variables A, B, etc. must be equal to the number of format specifiers (e.g., %s or %d) in the format argument. You can read all of the data in str to a single output variable as long as you use only one format specifier in the command. See Example 4 and Example 5 below.

The table "Formats for strread" lists the valid format specifiers. More information on using formats is available under Formats in the Remarks section below.

[A, B, ...] = strread('str', 'format', N) reads data from str reusing the format string N times, where N is an integer greater than zero. If N is -1, strread reads the entire string. When str contains only numeric data, you can set format to the empty string (''). See Example 3 below.

[A, B, ...] = strread('str', 'format', N, param, value, ...) customizes strread using param/value pairs, as listed in the table "Parameters and Values for strread" below. When str contains only numeric data, you can set format to the empty string (''). The N argument is optional and may be omitted entirely. See Example 7 below.

Formats for strread 
Format
Action
Output
Literals
(ordinary characters)
Ignore the matching characters. For example, in a string that has Dept followed by a number (for department number), to skip the Dept and read only the number, use 'Dept' in the format string.
None
%d
Read a signed integer value.
Double array
%u
Read an integer value.
Double array
%f
Read a floating-point value.
Double array
%s
Read a white-space separated string.
Cell array of strings
%q
Read a double quoted string, ignoring the quotes.
Cell array of strings
%c
Read characters, including white space.
Character array
%[...]
Read the longest string containing characters specified in the brackets.
Cell array of strings
%[^...]
Read the longest nonempty string containing characters that are not specified in the brackets.
Cell array of strings
%*...
Ignore the characters following *. See Example 8 below.
No output
%w...
Read field width specified by w. The %f format supports %w.pf, where w is the field width and p is the precision.



Parameters and Values for strread 
param
value
Action
whitespace
\* where * can be
Treats vector of characters, *, as white space. Default is \b\r\n\t.

b
f
n
r
t
\\
\'' or ''
%%
Backspace
Form feed
New line
Carriage return
Horizontal tab
Backslash
Single quotation mark
Percent sign
delimiter
Delimiter character
Specifies delimiter character. Default is one or more whitespace characters.
expchars
Exponent characters
Default is eEdD.
bufsize
Positive integer
Specifies the maximum string length, in bytes. Default is 4095.
commentstyle
matlab
Ignores characters after %.
commentstyle
shell
Ignores characters after #.
commentstyle
c
Ignores characters between /* and */.
commentstyle
c++
Ignores characters after //.

Remarks

Delimiters

If your data uses a character other than a space as a delimiter, you must use the strread parameter 'delimiter' to specify the delimiter. For example, if the string str used a semicolon as a delimiter, you would use this command:

Formats

The format string determines the number and types of return arguments. The number of return arguments must match the number of conversion specifiers in the format string.

The strread function continues reading str until the entire string is read. If there are fewer format specifiers than there are entities in str, strread reapplies the format specifiers, starting over at the beginning. See Example 5 below.

The format string supports a subset of the conversion specifiers and conventions of the C language fscanf routine. White-space characters in the format string are ignored.

Preserving White-Space

If you want to preserve leading and trailing spaces in a string, use the whitespace parameter as shown here:

Examples

Example 1

Read numeric data into a 1-by-5 vector:

Example 2

Read numeric data into separate scalar variables:

Example 3

Read the only first three numbers in the string, also formatting as floating point:

Example 4

Truncate the data to one decimal digit by specifying format %3.1f. The second specifier, %*1d, tells strread not to read in the remaining decimal digit:

Example 5

Read six numbers into two variables, reusing the format specifiers:

Example 6

Read string and numeric data to two output variables. Ignore commas in the input string:

Example 7

Read the string used in the last example, but this time delimiting with commas instead of spaces:

Example 8

Read selected portions of the input string:

Example 9

Read the string into two vectors, restricting the Answer values to T and F. Also note that two delimiters (comma and space) are used here:

See Also

textread, sscanf


Previous page  strncmpi strrep Next page

© 1994-2005 The MathWorks, Inc.