MATLAB Function Reference    
strread

Read formatted data from a string

Syntax

Description

The first four syntaxes are used on strings containing only numeric data. If the input string, str, contains any text data, an error is generated.

A = strread('str') reads numeric data from the string, str, into the single variable A.

A = strread('str','',N) reads N lines of numeric data, where N is an integer greater than zero. If N is -1, strread reads the entire string.

A = strread('str','',param,value,...) customizes strread using param/value pairs, as listed in the table below.

A = strread('str','',N,param,value,...) reads N lines and customizes the strread using param/value pairs.

The next four syntaxes can be used on numeric or nonnumeric data. In this case, strread reads data from the string, str, into the variables A, B, C, and so on, using the specified format.

The type of each return argument is given by the format string. The number of return arguments must match the number of conversion specifiers in the format string. If there are fewer fields in the string than matching conversion specifiers in the format string, an error is generated.

The format string determines the number and types of return arguments. The number of return arguments is the number of items in the format string. The format string supports a subset of the conversion specifiers and conventions of the C language fscanf routine. Values for the format string are listed in the table below. Whitespace characters in the format string are ignored.

[A,B,C,...] = strread('str','format') reads data from the string, str, into the variables A, B, C, and so on, using the specified format, until the entire string is read.

format
Action
Output
Literals
(ordinary characters)
Ignore the matching characters. For example, in a file 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 whitespace-separated string.
Cell array of strings
%q
Read a string, which could be in double quotes.
Cell array of strings. Does not include the double quotes.
%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 non-empty string containing characters that are not specified in the brackets.
Cell array of strings
%*...
instead of %
Ignore the matching characters specified by *.
No output
%w...
instead of %
Read field width specified by w. The %f format supports %w.pf, where w is the field width and p is the precision.

[A,B,C,...] = strread('str','format',N) reads the data, reusing the format string N times, where N is an integer greater than zero. If N is -1, strread reads the entire string.

[A,B,C,...] = strread('str','format',param,value,...) customizes strread using param/value pairs, as listed in the table below.

[A,B,C,...] = strread('str','format',N,param,value,...) reads the data, reusing the format string N times and customizes the strread using param/value pairs.

param
value
Action
whitespace
\* where * can be:
Treats vector of characters, *, as whitespace. 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 none.
expchars
Exponent characters
Default is eEdD.
bufsize
positive integer
Specifies the maximum string length, in bytes. Default is 4095.
headerlines
positive integer
Ignores the specified number of lines at the beginning of the file.
commentstyle
matlab
Ignores characters after %
commentstyle
shell
Ignores characters after #.
commentstyle
c
Ignores characters between /* and */.
commentstyle
c++
Ignores characters after //.

Remarks

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.

Examples

See Also

textread, sscanf


  strncmpi strrep