annotate toolboxes/FullBNT-1.0.7/netlab3.3/datread.m @ 0:cc4b1211e677 tip

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