wolffd@0
|
1 function [x, t, nin, nout, ndata] = datread(filename)
|
wolffd@0
|
2 %DATREAD Read data from an ascii file.
|
wolffd@0
|
3 %
|
wolffd@0
|
4 % Description
|
wolffd@0
|
5 %
|
wolffd@0
|
6 % [X, T, NIN, NOUT, NDATA] = DATREAD(FILENAME) reads from the file
|
wolffd@0
|
7 % FILENAME and returns a matrix X of input vectors, a matrix T of
|
wolffd@0
|
8 % target vectors, and integers NIN, NOUT and NDATA specifying the
|
wolffd@0
|
9 % number of inputs, the number of outputs and the number of data points
|
wolffd@0
|
10 % respectively.
|
wolffd@0
|
11 %
|
wolffd@0
|
12 % The format of the data file is as follows: the first row contains the
|
wolffd@0
|
13 % string NIN followed by the number of inputs, the second row contains
|
wolffd@0
|
14 % the string NOUT followed by the number of outputs, and the third row
|
wolffd@0
|
15 % contains the string NDATA followed by the number of data vectors.
|
wolffd@0
|
16 % Subsequent lines each contain one input vector followed by one output
|
wolffd@0
|
17 % vector, with individual values separated by spaces.
|
wolffd@0
|
18 %
|
wolffd@0
|
19 % See also
|
wolffd@0
|
20 % nin 2 nout 1 ndata 4 0.000000e+00 0.000000e+00
|
wolffd@0
|
21 % 1.000000e+00 0.000000e+00 1.000000e+00 0.000000e+00
|
wolffd@0
|
22 % 1.000000e+00 0.000000e+00 0.000000e+00 1.000000e+00
|
wolffd@0
|
23 % 1.000000e+00 1.000000e+00 See Also
|
wolffd@0
|
24 % DATWRITE
|
wolffd@0
|
25 %
|
wolffd@0
|
26
|
wolffd@0
|
27 % Copyright (c) Ian T Nabney (1996-2001)
|
wolffd@0
|
28
|
wolffd@0
|
29 fid = fopen(filename, 'rt');
|
wolffd@0
|
30 if fid == -1
|
wolffd@0
|
31 error('Failed to open file.')
|
wolffd@0
|
32 end
|
wolffd@0
|
33
|
wolffd@0
|
34 % Read number of inputs
|
wolffd@0
|
35 s1 = fscanf(fid, '%s', 1);
|
wolffd@0
|
36 if ~strcmp(s1, 'nin')
|
wolffd@0
|
37 fclose(fid);
|
wolffd@0
|
38 error('String ''nin'' not found')
|
wolffd@0
|
39 end
|
wolffd@0
|
40 nin = fscanf(fid, '%d\n', 1);
|
wolffd@0
|
41 if ~isnumeric(nin)
|
wolffd@0
|
42 fclose(fid);
|
wolffd@0
|
43 error('No number for nin')
|
wolffd@0
|
44 end
|
wolffd@0
|
45 if nin < 0 | round(nin) ~= nin
|
wolffd@0
|
46 fclose(fid);
|
wolffd@0
|
47 error('nin must be a non-negative integer')
|
wolffd@0
|
48 end
|
wolffd@0
|
49
|
wolffd@0
|
50 % Read number of outputs
|
wolffd@0
|
51 s2 = fscanf(fid, '%s', 1);
|
wolffd@0
|
52 if ~strcmp(s2, 'nout')
|
wolffd@0
|
53 fclose(fid);
|
wolffd@0
|
54 error('String ''nout'' not found')
|
wolffd@0
|
55 end
|
wolffd@0
|
56 nout = fscanf(fid, '%d\n', 1);
|
wolffd@0
|
57 if ~isnumeric(nout)
|
wolffd@0
|
58 fclose(fid);
|
wolffd@0
|
59 error('No number for nout')
|
wolffd@0
|
60 end
|
wolffd@0
|
61 if nout < 0 | round(nout) ~= nout
|
wolffd@0
|
62 fclose(fid);
|
wolffd@0
|
63 error('nout must be a non-negative integer')
|
wolffd@0
|
64 end
|
wolffd@0
|
65
|
wolffd@0
|
66 % Read number of data values
|
wolffd@0
|
67 s3 = fscanf(fid, '%s', 1);
|
wolffd@0
|
68 if ~strcmp(s3, 'ndata')
|
wolffd@0
|
69 fclose(fid);
|
wolffd@0
|
70 error('String ''ndata'' not found')
|
wolffd@0
|
71 end
|
wolffd@0
|
72 ndata = fscanf(fid, '%d\n', 1);
|
wolffd@0
|
73 if ~isnumeric(ndata)
|
wolffd@0
|
74 fclose(fid);
|
wolffd@0
|
75 error('No number for ndata')
|
wolffd@0
|
76 end
|
wolffd@0
|
77 if ndata < 0 | round(ndata) ~= ndata
|
wolffd@0
|
78 fclose(fid);
|
wolffd@0
|
79 error('ndata must be a non-negative integer')
|
wolffd@0
|
80 end
|
wolffd@0
|
81
|
wolffd@0
|
82 % The following line reads all of the remaining data to the end of file.
|
wolffd@0
|
83 temp = fscanf(fid, '%f', inf);
|
wolffd@0
|
84
|
wolffd@0
|
85 % Check that size of temp is correct
|
wolffd@0
|
86 if size(temp, 1) * size(temp,2) ~= (nin+nout) * ndata
|
wolffd@0
|
87 fclose(fid);
|
wolffd@0
|
88 error('Incorrect number of elements in file')
|
wolffd@0
|
89 end
|
wolffd@0
|
90
|
wolffd@0
|
91 temp = reshape(temp, nin + nout, ndata)';
|
wolffd@0
|
92 x = temp(:, 1:nin);
|
wolffd@0
|
93 t = temp(:, nin + 1 : nin + nout);
|
wolffd@0
|
94
|
wolffd@0
|
95 flag = fclose(fid);
|
wolffd@0
|
96 if flag == -1
|
wolffd@0
|
97 error('Failed to close file.')
|
wolffd@0
|
98 end
|
wolffd@0
|
99
|