| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
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.
format |
String enclosed in single quotation marks that describes each type of element (field). Includes one or more of the following specifiers.
Optionally:
| |||||||||||||||||||||||||||
sizeA |
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. | |||||||||||||||||||||||||||
str |
Character string. |
A |
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. |
count |
Number of elements sscanf reads into A. |
errmsg |
An error message string when sscanf cannot open the specified file. Otherwise, an empty string. |
nextindex |
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') % returns 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') % returns ans = -8Read 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
numsMATLAB returns:
nums =
45 6
7 89![]() | ss2tf | stairs | ![]() |

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 |