Read formatted data from string
A = sscanf(str, format)
A = sscanf(str, format, sizeA)
[A, count] = sscanf(...)
[A, count, errmsg] = sscanf(...)
[A, count, errmsg, nextindex] = sscanf(...)
A = sscanf(str, format) reads data from string str, converts it according to the format, and returns the results in array A. The sscanf function reapplies the format until either reaching the end of str or failing to match the format. If sscanf cannot match the format to the data, it reads only the portion that matches into A and stops processing. If str is a character array with more than one row, sscanf reads the characters in column order.
A = sscanf(str, format, sizeA) reads sizeA elements into A, where sizeA can be an integer or can have the form [m,n].
[A, count] = sscanf(...) returns the number of elements that sscanf successfully reads.
[A, count, errmsg] = sscanf(...) returns an error message string when the operation is unsuccessful. Otherwise, errmsg is an empty string.
[A, count, errmsg, nextindex] = sscanf(...) returns one more than the number of characters scanned in str.
String enclosed in single quotation marks that describes each type of element (field). Includes one or more of the following specifiers.
Dimensions of the output array A. Specify in one of the following forms:
When the format includes %s, A can contain more than n columns. n refers to elements, not characters.
An array. If the format includes:
If MATLAB cannot match the input to the format, and the format contains both numeric and character specifiers, A can be numeric or character. The class of A depends on the values MATLAB reads before processing stops.
Number of elements sscanf reads into A.
An error message string when sscanf cannot open the specified file. Otherwise, an empty string.
sscanf counts the number of characters sscanf reads from str, and then adds one.
Read multiple floating-point values from a string:
s = '2.7183 3.1416'; A = sscanf(s,'%f') A = 2.7183 3.1416
Read an octal integer from a string, identified by the '0' prefix, using %i to preserve the sign:
sscanf('-010','%i') ans = -8
Read numeric values from a two-dimensional character array. By default, sscanf reads characters in column order. To preserve the original order of the values, read one row at a time.
mixed = ['abc 45 6 ghi'; 'def 7 89 jkl']; [nrows, ncols] = size(mixed); for k = 1:nrows nums(k,:) = sscanf(mixed(k,:), '%*s %d %d %*s', [1, inf]); end; % type the variable name to see the result nums = 45 6 7 89
sscanf finds one match for %s
[str count] = sscanf('ThisIsOneString', '%s') str = ThisIsOneString count = 1
sscanf finds four matches for %s. Because it does not match space characters, there are no spaces in the output string:
[str count] = sscanf('These Are Four Strings', '%s') str = TheseAreFourStrings count = 4
sscanf finds five word matches for %s and four space character matches for %c. Because the %c specifier does match a space character, the output string does include spaces:
[str count] = sscanf('Five strings and four spaces', '%s%c') str = Five strings and four spaces count = 9
sscanf finds three word matches for %s and two numeric matches for %d. Because the format specifier has a mixed %d and %s format, sscanf converts all nonnumeric characters to numeric:
[str count] = sscanf('5 strings and 4 spaces', '%d%s%s%d%s'); str' Columns 1 through 9 5 115 116 114 105 110 103 115 97 Columns 10 through 18 110 100 4 115 112 97 99 101 115 count count = 5
[str, count] = sscanf('one two three', '%c') str = one two three count = 13 [str, count] = sscanf('one two three', '%13c') str = one two three count = 1
[str, count] = sscanf('one two three', '%s') str = onetwothree count = 3 [str, count] = sscanf('one two three', '%1s') str = onetwothree count = 11
tempString = '78°F 72°F 64°F 66°F 49°F'; degrees = char(176); tempNumeric = sscanf(tempString, ['%d' degrees 'F'])' tempNumeric = 78 72 64 66 49