Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

strread

(Not recommended) Read formatted data from string

strread is not recommended. Use textscan instead.

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 character vector str into a 1-by-N vector A, where N equals the number of whitespace-separated numbers in str. Use this form only with character vectors containing numeric data. See Example 1 below.

[A, B, ...] = strread('str') reads numeric data from the character vector 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 character vectors 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 Tips section below.

[A, B, ...] = strread('str', 'format', N) reads data from str reusing the format character vector N times, where N is an integer greater than zero. If N is -1, strread reads the entire character vector. When str contains only numeric data, you can set format to the empty character vector (''). 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 character vector (''). 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 character vector that has Dept followed by a number (for department number), to skip the Dept and read only the number, use 'Dept' in the format character vector.

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 character vector.

Cell array of character vectors

%q

Read a double quoted character vector, ignoring the quotes.

Cell array of character vectors

%c

Read characters, including white space.

Character array

%[...]

Read the longest character vector containing characters specified in the brackets.

Cell array of character vectors

%[^...]

Read the longest nonempty character vector containing characters that are not specified in the brackets.

Cell array of character vectors

%*...

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 character vector length, in bytes. Default is 4095.

commentstylematlab

Ignores characters after %.

commentstyleshell

Ignores characters after #.

commentstylec

Ignores characters between /* and */.

commentstylec++

Ignores characters after //.

emptyvalueValue to return for empty numeric fields in delimited filesDefault is 0.

Examples

Example 1

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.2700

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 character vector, 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.5700

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:

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.2000

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 character vector and numeric data to two output variables. Ignore commas in the input character vector:

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 character vector 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 character vector:

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 character vector 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'

Tips

If you terminate the input character vector 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
   NaN

If you remove the \n from the input character vector of this example, array A continues to be a 2-by-1 array, but B and C are now 1-by-1.

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 character vector 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',';')

Formats

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

The strread function continues reading str until the entire character vector 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 character vector supports a subset of the conversion specifiers and conventions of the C language fscanf routine. White-space characters in the format character vector are ignored.

Preserving White-Space

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

str = '   An  example      of preserving     spaces      ';

strread(str, '%s', 'whitespace', '')
ans = 
    '   An  example      of preserving     spaces      '

See Also

|

Introduced before R2006a

Was this topic helpful?