MATLAB Function Reference |
Read formatted data from string
Syntax
A = strread('str') [A, B, ...] = strread('str') [A, B, ...] = strread('str', 'format') [A, B, ...] = strread('str', 'format', N) [A, B, ...] = strread('str', 'format', N, param, value, ...)
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.
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. |
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:
str = ' An example of preserving spaces '; strread(str, '%s', 'whitespace', '') ans = ' An example of preserving spaces '
Example 1
Read numeric data into a 1-by-5 vector:
Example 2
Read numeric data into separate scalar variables:
[a b c d e] = strread('0.41 8.24 3.57 6.24 9.27') a = 0.4100 b = 8.2400 c = 3.5700 d = 6.2400 e = 9.2700
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:
[a b] = strread('0.41 8.24 3.57 6.24 9.27 3.29', '%f %f') a = 0.4100 3.5700 9.2700 b = 8.2400 6.2400 3.2900
Example 6
Read string and numeric data to two output variables. Ignore commas in the input string:
str = 'Section 4, Page 7, Line 26'; [name value] = strread(str, '%s %d,') name = 'Section' 'Page' 'Line' value = 4 7 26
Example 7
Read the string used in the last example, but this time delimiting with commas instead of spaces:
str = 'Section 4, Page 7, Line 26'; [a b c] = strread(str, '%s %s %s', 'delimiter', ',') a = 'Section 4' b = 'Page 7' c = 'Line 26'
Example 8
Read selected portions of the input string:
str = '<table border=5 width="100%" cellspacing=0>'; [border width space] = strread(str, ... '%*s%*s %c %*s "%4s" %*s %c', 'delimiter', '= ') border = 5 width = '100%' space = 0
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:
str = 'Answer_1: T, Answer_2: F, Answer_3: F'; [a b] = strread(str, '%s %[TF]', 'delimiter', ', ') a = 'Answer_1:' 'Answer_2:' 'Answer_3:' b = 'T' 'F' 'F'
See Also
strncmpi | strrep |
© 1994-2005 The MathWorks, Inc.