fscanf

Returns result R after reading a formatted data from file handle, f, specified by template t.

Syntax

R = fscanf(f, t)

R = fscanf(f, t, size)

Inputs

f
File handle.
Type: integer
t
Template to read formatted data. Valid formats are '%d', '%f', '%g and '%s'.
Type: string
size (optional)
Argument which specifies size of the output. size maybe 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 with the second element having a value of infinity, all data is read and returned in a matrix with the number of rows as specified by the first element.
Type: Positive integer | matrix

Examples

Reads all data from file, where the data is '1/1/2001 10:30:00 2/5/2003 1:15:45'
f = fopen('fscanf1.txt');
R = fscanf(f, '%d/%d/%d %d:%d:%d')
R = [Matrix] 12 x 1
1
1
2001
10
30
0
2
5
2003
1
15
45
Specifies size of output. The input in the file is '5.48813502420671 5.92844616505317 7.15189365088008 8.44265744206496 6.02763370494358'
f = fopen('fscanf1.txt');
R = fscanf(f, '%f', 3)

R = [Matrix] 3 x 1
5.48813
5.92845
7.15189
Specifies size of output with a matrix of finite elements. The input in the file is '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'
f = fopen('fscanf1.txt');
R = fscanf(f, '%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
Specifies size of output with a matrix, reading as much data as possible. The input in the file is '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'
f = fopen('fscanf1.txt');
R = fscanf(f, '%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