sscanf
Reads formatted data from text.
Category: Language Fundamentals > Data Types
Syntax
A = sscanf(str, formatSpec)
Reads all the values that match the format.
A = sscanf(str, formatSpec, sizeA)
Reads at most sizeA values, or fills a matrix when sizeA is [m n].
[A, count] = sscanf(___)
Also returns how many values were read.
Description
sscanf is the reverse of sprintf. The format is applied repeatedly to the text, and reading stops at the first mismatch. Text between conversions in the format, like the x= in 'x=%f', must be present in the input. Whitespace in the format matches any amount of whitespace.
If nothing can be read, the result is empty. For plain conversion of a single number from text, str2double is simpler.
Input arguments
str- Character vector or string.
formatSpec- Conversions such as
%d,%f,%s,%c, plus literal text that must match. sizeA- Maximum number of values, or
[m n].
Output arguments
A- Column vector (or matrix) of the values read. If the format mixes numbers and text, characters are returned as their character codes.
count- Number of values read.
Example
sscanf('3 4 5', '%d')'
sscanf('x=2.5', 'x=%f')
[v, n] = sscanf('1 2 3', '%d')
sscanf('10:20', '%d:%d')'
Output
ans = 1×3
3 4 5
ans = 2.5000
v = 3×1
1
2
3
n = 3
ans = 1×2
10 20