| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
Note strread is not recommended. Use textscan to read data from a string. |
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, ...)
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 | Any from the list below: | Treats vector of characters, *, as white space. Default is \b\r\n\t. | |
| \b \n \r \t \\ %% '' | Backspace New line Carriage return Horizontal tab Backslash Percent sign Single quotation mark | ||
| 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 //. | |
| emptyvalue | Value to return for empty numeric fields in delimited files | Default is NaN. | |
If you terminate the input string with a newline character (\n), strread returns arrays of equal size by padding arrays of lesser size with the emptyvalue character:
[A,B,C] = strread(sprintf('5,7,1,9\n'),'%d%d%d', ...
'delimiter', ',', 'emptyvalue',NaN)
A =
5
9
B =
7
NaN
C =
1
NaNIf you remove the \n from the input string of this example, array A continues to be a 2-by-1 array, but B and C are now 1-by-1.
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:
[names, types, x, y, answer] = strread(str,'%s %s %f ...
%d %s','delimiter',';')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.
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 'Read numeric data into a 1-by-5 vector:
a = strread('0.41 8.24 3.57 6.24 9.27')
a =
0.4100 8.2400 3.5700 6.2400 9.2700Read 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.2700Read the only first three numbers in the string, also formatting as floating point:
a = strread('0.41 8.24 3.57 6.24 9.27', '%4.2f', 3)
a =
0.4100
8.2400
3.5700Truncate 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:
a = strread('0.41 8.24 3.57 6.24 9.27', '%3.1f %*1d')
a =
0.4000
8.2000
3.5000
6.2000
9.2000Read 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.2900Read 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
26Read 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'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 =
0Read 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'![]() | strncmp, strncmpi | strrep | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |