Daniel@0: function [x, t, nin, nout, ndata] = datread(filename) Daniel@0: %DATREAD Read data from an ascii file. Daniel@0: % Daniel@0: % Description Daniel@0: % Daniel@0: % [X, T, NIN, NOUT, NDATA] = DATREAD(FILENAME) reads from the file Daniel@0: % FILENAME and returns a matrix X of input vectors, a matrix T of Daniel@0: % target vectors, and integers NIN, NOUT and NDATA specifying the Daniel@0: % number of inputs, the number of outputs and the number of data points Daniel@0: % respectively. Daniel@0: % Daniel@0: % The format of the data file is as follows: the first row contains the Daniel@0: % string NIN followed by the number of inputs, the second row contains Daniel@0: % the string NOUT followed by the number of outputs, and the third row Daniel@0: % contains the string NDATA followed by the number of data vectors. Daniel@0: % Subsequent lines each contain one input vector followed by one output Daniel@0: % vector, with individual values separated by spaces. Daniel@0: % Daniel@0: % See also Daniel@0: % nin 2 nout 1 ndata 4 0.000000e+00 0.000000e+00 Daniel@0: % 1.000000e+00 0.000000e+00 1.000000e+00 0.000000e+00 Daniel@0: % 1.000000e+00 0.000000e+00 0.000000e+00 1.000000e+00 Daniel@0: % 1.000000e+00 1.000000e+00 See Also Daniel@0: % DATWRITE Daniel@0: % Daniel@0: Daniel@0: % Copyright (c) Ian T Nabney (1996-2001) Daniel@0: Daniel@0: fid = fopen(filename, 'rt'); Daniel@0: if fid == -1 Daniel@0: error('Failed to open file.') Daniel@0: end Daniel@0: Daniel@0: % Read number of inputs Daniel@0: s1 = fscanf(fid, '%s', 1); Daniel@0: if ~strcmp(s1, 'nin') Daniel@0: fclose(fid); Daniel@0: error('String ''nin'' not found') Daniel@0: end Daniel@0: nin = fscanf(fid, '%d\n', 1); Daniel@0: if ~isnumeric(nin) Daniel@0: fclose(fid); Daniel@0: error('No number for nin') Daniel@0: end Daniel@0: if nin < 0 | round(nin) ~= nin Daniel@0: fclose(fid); Daniel@0: error('nin must be a non-negative integer') Daniel@0: end Daniel@0: Daniel@0: % Read number of outputs Daniel@0: s2 = fscanf(fid, '%s', 1); Daniel@0: if ~strcmp(s2, 'nout') Daniel@0: fclose(fid); Daniel@0: error('String ''nout'' not found') Daniel@0: end Daniel@0: nout = fscanf(fid, '%d\n', 1); Daniel@0: if ~isnumeric(nout) Daniel@0: fclose(fid); Daniel@0: error('No number for nout') Daniel@0: end Daniel@0: if nout < 0 | round(nout) ~= nout Daniel@0: fclose(fid); Daniel@0: error('nout must be a non-negative integer') Daniel@0: end Daniel@0: Daniel@0: % Read number of data values Daniel@0: s3 = fscanf(fid, '%s', 1); Daniel@0: if ~strcmp(s3, 'ndata') Daniel@0: fclose(fid); Daniel@0: error('String ''ndata'' not found') Daniel@0: end Daniel@0: ndata = fscanf(fid, '%d\n', 1); Daniel@0: if ~isnumeric(ndata) Daniel@0: fclose(fid); Daniel@0: error('No number for ndata') Daniel@0: end Daniel@0: if ndata < 0 | round(ndata) ~= ndata Daniel@0: fclose(fid); Daniel@0: error('ndata must be a non-negative integer') Daniel@0: end Daniel@0: Daniel@0: % The following line reads all of the remaining data to the end of file. Daniel@0: temp = fscanf(fid, '%f', inf); Daniel@0: Daniel@0: % Check that size of temp is correct Daniel@0: if size(temp, 1) * size(temp,2) ~= (nin+nout) * ndata Daniel@0: fclose(fid); Daniel@0: error('Incorrect number of elements in file') Daniel@0: end Daniel@0: Daniel@0: temp = reshape(temp, nin + nout, ndata)'; Daniel@0: x = temp(:, 1:nin); Daniel@0: t = temp(:, nin + 1 : nin + nout); Daniel@0: Daniel@0: flag = fclose(fid); Daniel@0: if flag == -1 Daniel@0: error('Failed to close file.') Daniel@0: end Daniel@0: