comparison toolboxes/FullBNT-1.0.7/netlab3.3/datread.m @ 0:e9a9cd732c1e tip

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