sscanf
Returns result R after reading a formatted string s, specified by template t.
Syntax
R = sscanf(s, t)
R = sscanf(s, t, size)
Inputs
- s
- String to read formatted data from.
- t
- Template to read formatted data. Valid formats are '%d', '%f', '%g' and '%s'. Valid options for skipping a specific format are '%*d', '%*f', '%*g' and '%*s'.
- size
- Optional argument that specifies size of the output. size can be a positive scalar or a matrix with at least two, positive, real elements. If size is a scalar with a value of 'inf', all data is read and a column vector is returned. If size is a matrix and the second element has a value of infinity. All data is read and returned in a matrix with the number of rows as specified by the first element.
Outputs
- R
- Type: matrix
Examples
Read all data from a given string:
R = sscanf('1/1/2001 10:30:00 2/5/2003 1:15:45', '%d/%d/%d %d:%d:%d')
R = [Matrix] 12 x 1
1
1
2001
10
30
0
2
5
2003
1
15
45
Specify the size of output with a scalar for reading formatted
string:
R = sscanf('5.48813502420671 5.92844616505317 7.15189365088008 8.44265744206496 6.02763370494358', '%f', 3)
R = [Matrix] 3 x 1
5.48813
5.92845
7.15189
Specify the size of output with a matrix of finite
elements:
R = sscanf('1 1.77 0.96 2 1.95 0.83 3 2.18 0.65 4 2.75 1.42 5 3.16 5.53 6 3.25 4.19 7 2.86 2.82', '%d %f %f', [3 10])
R = [Matrix] 3 x 7
1.00000 2.00000 3.00000 4.00000 5.00000 6.00000 7.00000
1.77000 1.95000 2.18000 2.75000 3.16000 3.25000 2.86000
0.96000 0.83000 0.65000 1.42000 5.53000 4.19000 2.82000
Specify the size of output with a matrix and read as much data as
possible:
R = sscanf('1 1.77 0.96 2 1.95 0.83 3 2.18 0.65 4 2.75 1.42 5 3.16 5.53 6 3.25 4.19 7 2.86 2.82', '%d %f %f', [10 inf])
R = [Matrix] 10 x 2
1.00000 2.75000
1.77000 1.42000
0.96000 5.00000
2.00000 3.16000
1.95000 5.53000
0.83000 6.00000
3.00000 3.25000
2.18000 4.19000
0.65000 7.00000
4.00000 2.86000
Skip formats:
s = 'WHEEL-RAIL CONTACT, LEFT WHEEL, 1 CONTACT PATCH';
R = sscanf(s, 'WHEEL-R%*s CONTACT, %*s WHEEL, %d')
R = 1