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