Daniel@0: function sData = som_read_data(filename, varargin) Daniel@0: Daniel@0: %SOM_READ_DATA Read data from an ascii file in SOM_PAK format. Daniel@0: % Daniel@0: % sD = som_read_data(filename, dim, [missing]) Daniel@0: % sD = som_read_data(filename, [missing]) Daniel@0: % Daniel@0: % sD = som_read_data('system.data'); Daniel@0: % sD = som_read_data('system.data',10); Daniel@0: % sD = som_read_data('system.data','*'); Daniel@0: % sD = som_read_data('system.data',10,'*'); Daniel@0: % Daniel@0: % Input and output arguments ([]'s are optional): Daniel@0: % filename (string) input file name Daniel@0: % dim (scalar) input space dimension Daniel@0: % [missing] (string) string which indicates a missing component Daniel@0: % value, 'NaN' by default Daniel@0: % Daniel@0: % sD (struct) data struct Daniel@0: % Daniel@0: % Reads data from an ascii file. The file must be in SOM_PAK format, Daniel@0: % except that it may lack the input space dimension from the first Daniel@0: % line. Daniel@0: % Daniel@0: % For more help, try 'type som_read_data' or check out online documentation. Daniel@0: % See also SOM_WRITE_DATA, SOM_READ_COD, SOM_WRITE_COD, SOM_DATA_STRUCT. Daniel@0: Daniel@0: %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: % Daniel@0: % som_read_data Daniel@0: % Daniel@0: % PURPOSE Daniel@0: % Daniel@0: % Reads data from an ascii file in SOM_PAK format. Daniel@0: % Daniel@0: % SYNTAX Daniel@0: % Daniel@0: % sD = som_read_data(filename) Daniel@0: % sD = som_read_data(..., dim) Daniel@0: % sD = som_read_data(..., 'missing') Daniel@0: % sD = som_read_data(..., dim, 'missing') Daniel@0: % Daniel@0: % DESCRIPTION Daniel@0: % Daniel@0: % This function is offered for compatibility with SOM_PAK, a SOM software Daniel@0: % package in C. It reads data from a file in SOM_PAK format. Daniel@0: % Daniel@0: % The SOM_PAK data file format is as follows. The first line must Daniel@0: % contain the input space dimension and nothing else. The following Daniel@0: % lines are comment lines, empty lines or data lines. Unlike programs Daniel@0: % in SOM_PAK, this function can also determine the input dimension Daniel@0: % from the first data lines, if the input space dimension line is Daniel@0: % missing. Note that the SOM_PAK format is not fully supported: data Daniel@0: % vector 'weight' and 'fixed' properties are ignored (they are treated Daniel@0: % as labels). Daniel@0: % Daniel@0: % Each data line contains one data vector and its labels. From the beginning Daniel@0: % of the line, first are values of the vector components separated by Daniel@0: % whitespaces, then labels also separated by whitespaces. If there are Daniel@0: % missing values in the vector, the missing value marker needs to be Daniel@0: % specified as the last input argument ('NaN' by default). The missing Daniel@0: % values are stored as NaNs in the data struct. Daniel@0: % Daniel@0: % Comment lines start with '#'. Comment lines as well as empty lines are Daniel@0: % ignored, except if the comment lines that start with '#n' or '#l'. In that Daniel@0: % case the line should contain names of the vector components or label names Daniel@0: % separated by whitespaces. Daniel@0: % Daniel@0: % NOTE: The minimum value Matlab is able to deal with (realmax) Daniel@0: % should not appear in the input file. This is because function sscanf is Daniel@0: % not able to read NaNs: the NaNs are in the read phase converted to value Daniel@0: % realmax. Daniel@0: % Daniel@0: % REQUIRED INPUT ARGUMENTS Daniel@0: % Daniel@0: % filename (string) input filename Daniel@0: % Daniel@0: % OPTIONAL INPUT ARGUMENTS Daniel@0: % Daniel@0: % dim (scalar) input space dimension Daniel@0: % missing (string) string used to denote missing components (NaNs); Daniel@0: % default is 'NaN' Daniel@0: % Daniel@0: % OUTPUT ARGUMENTS Daniel@0: % Daniel@0: % sD (struct) the resulting data struct Daniel@0: % Daniel@0: % EXAMPLES Daniel@0: % Daniel@0: % The basic usage is: Daniel@0: % sD = som_read_data('system.data'); Daniel@0: % Daniel@0: % If you know the input space dimension beforehand, and the file does Daniel@0: % not contain it on the first line, it helps if you specify it as the Daniel@0: % second argument: Daniel@0: % sD = som_read_data('system.data',9); Daniel@0: % Daniel@0: % If the missing components in the data are marked with some other Daniel@0: % characters than with 'NaN', you can specify it with the last argument: Daniel@0: % sD = som_read_data('system.data',9,'*') Daniel@0: % sD = som_read_data('system.data','NaN') Daniel@0: % Daniel@0: % Here's an example data file: Daniel@0: % Daniel@0: % 5 Daniel@0: % #n one two three four five Daniel@0: % #l ID Daniel@0: % 10 2 3 4 5 1stline label Daniel@0: % 0.4 0.3 0.2 0.5 0.1 2ndline label1 label2 Daniel@0: % # comment line: missing components are indicated by 'x':s Daniel@0: % 1 x 1 x 1 3rdline missing_components Daniel@0: % x 1 2 2 2 Daniel@0: % x x x x x 5thline emptyline Daniel@0: % Daniel@0: % SEE ALSO Daniel@0: % Daniel@0: % som_write_data Writes data structs/matrices to a file in SOM_PAK format. Daniel@0: % som_read_cod Read a map from a file in SOM_PAK format. Daniel@0: % som_write_cod Writes data struct into a file in SOM_PAK format. Daniel@0: % som_data_struct Creates data structs. Daniel@0: Daniel@0: % Copyright (c) 1997-2000 by the SOM toolbox programming team. Daniel@0: % http://www.cis.hut.fi/projects/somtoolbox/ Daniel@0: Daniel@0: % Version 1.0beta ecco 221097 Daniel@0: % Version 2.0beta ecco 060899, juuso 151199 Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% check arguments Daniel@0: Daniel@0: error(nargchk(1, 3, nargin)) % check no. of input args is correct Daniel@0: Daniel@0: dont_care = 'NaN'; % default don't care string Daniel@0: comment_start = '#'; % the char a SOM_PAK command line starts with Daniel@0: comp_name_line = '#n'; % string denoting a special command line, Daniel@0: % which contains names of each component Daniel@0: label_name_line = '#l'; % string denoting a special command line, Daniel@0: % which contains names of each label Daniel@0: block_size = 1000; % block size used in file read Daniel@0: Daniel@0: kludge = num2str(realmax, 100); % used in sscanf Daniel@0: Daniel@0: Daniel@0: % open input file Daniel@0: Daniel@0: fid = fopen(filename); Daniel@0: if fid < 0 Daniel@0: error(['Cannot open ' filename]); Daniel@0: end Daniel@0: Daniel@0: % process input arguments Daniel@0: Daniel@0: if nargin == 2 Daniel@0: if isstr(varargin{1}) Daniel@0: dont_care = varargin{1}; Daniel@0: else Daniel@0: dim = varargin{1}; Daniel@0: end Daniel@0: elseif nargin == 3 Daniel@0: dim = varargin{1}; Daniel@0: dont_care = varargin{2}; Daniel@0: end Daniel@0: Daniel@0: % if the data dimension is not specified, find out what it is Daniel@0: Daniel@0: if nargin == 1 | (nargin == 2 & isstr(varargin{1})) Daniel@0: Daniel@0: fpos1 = ftell(fid); c1 = 0; % read first non-comment line Daniel@0: while c1 == 0, Daniel@0: line1 = strrep(fgetl(fid), dont_care, kludge); Daniel@0: [l1, c1] = sscanf(line1, '%f '); Daniel@0: end Daniel@0: Daniel@0: fpos2 = ftell(fid); c2 = 0; % read second non-comment line Daniel@0: while c2 == 0, Daniel@0: line2 = strrep(fgetl(fid), dont_care, kludge); Daniel@0: [l2, c2] = sscanf(line2, '%f '); Daniel@0: end Daniel@0: Daniel@0: if (c1 == 1 & c2 ~= 1) | (c1 == c2 & c1 == 1 & l1 == 1) Daniel@0: dim = l1; Daniel@0: fseek(fid, fpos2, -1); Daniel@0: elseif (c1 == c2) Daniel@0: dim = c1; Daniel@0: fseek(fid, fpos1, -1); Daniel@0: warning on Daniel@0: warning(['Automatically determined data dimension is ' ... Daniel@0: num2str(dim) '. Is it correct?']); Daniel@0: else Daniel@0: error(['Invalid header line: ' line1]); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % check the dimension is valid Daniel@0: Daniel@0: if dim < 1 | dim ~= round(dim) Daniel@0: error(['Illegal data dimension: ' num2str(dim)]); Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% read data Daniel@0: Daniel@0: sData = som_data_struct(zeros(1, dim), 'name', filename); Daniel@0: lnum = 0; % data vector counter Daniel@0: data_temp = zeros(block_size, dim); Daniel@0: labs_temp = cell(block_size, 1); Daniel@0: comp_names = sData.comp_names; Daniel@0: label_names = sData.label_names; Daniel@0: form = [repmat('%g',[1 dim-1]) '%g%[^ \t]']; Daniel@0: Daniel@0: limit = block_size; Daniel@0: while 1, Daniel@0: li = fgetl(fid); % read next line Daniel@0: if ~isstr(li), break, end; % is this the end of file? Daniel@0: Daniel@0: % all missing vectors are replaced by value realmax because Daniel@0: % sscanf is not able to read NaNs Daniel@0: li = strrep(li, dont_care, kludge); Daniel@0: [data, c, err, n] = sscanf(li, form); Daniel@0: if c < dim % if there were less numbers than dim on the input file line Daniel@0: if c == 0 Daniel@0: if strncmp(li, comp_name_line, 2) % component name line? Daniel@0: li = strrep(li(3:end), kludge, dont_care); i = 0; c = 1; Daniel@0: while c Daniel@0: [s, c, e, n] = sscanf(li, '%s%[^ \t]'); Daniel@0: if ~isempty(s), i = i + 1; comp_names{i} = s; li = li(n:end); end Daniel@0: end Daniel@0: Daniel@0: if i ~= dim Daniel@0: error(['Illegal number of component names: ' num2str(i) ... Daniel@0: ' (dimension is ' num2str(dim) ')']); Daniel@0: end Daniel@0: elseif strncmp(li, label_name_line, 2) % label name line? Daniel@0: li = strrep(li(3:end), kludge, dont_care); i = 0; c = 1; Daniel@0: while c Daniel@0: [s, c, e, n] = sscanf(li, '%s%[^ \t]'); Daniel@0: if ~isempty(s), i = i + 1; label_names{i} = s; li = li(n:end); end Daniel@0: end Daniel@0: elseif ~strncmp(li, comment_start, 1) % not a comment, is it error? Daniel@0: [s, c, e, n] = sscanf(li, '%s%[^ \t]'); Daniel@0: if c Daniel@0: error(['Invalid vector on input file data line ' ... Daniel@0: num2str(lnum+1) ': [' deblank(li) ']']), Daniel@0: end Daniel@0: end Daniel@0: else Daniel@0: error(['Only ' num2str(c) ' vector components on input file data line ' ... Daniel@0: num2str(lnum+1) ' (dimension is ' num2str(dim) ')']); Daniel@0: end Daniel@0: Daniel@0: else Daniel@0: Daniel@0: lnum = lnum + 1; % this was a line containing data vector Daniel@0: data_temp(lnum, 1:dim) = data'; % add data to struct Daniel@0: Daniel@0: if lnum == limit % reserve more memory if necessary Daniel@0: data_temp(lnum+1:lnum+block_size, 1:dim) = zeros(block_size, dim); Daniel@0: [dummy nl] = size(labs_temp); Daniel@0: labs_temp(lnum+1:lnum+block_size,1:nl) = cell(block_size, nl); Daniel@0: limit = limit + block_size; Daniel@0: end Daniel@0: Daniel@0: % read labels Daniel@0: Daniel@0: if n < length(li) Daniel@0: li = strrep(li(n:end), kludge, dont_care); i = 0; n = 1; c = 1; Daniel@0: while c Daniel@0: [s, c, e, n_new] = sscanf(li(n:end), '%s%[^ \t]'); Daniel@0: if c, i = i + 1; labs_temp{lnum, i} = s; n = n + n_new - 1; end Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % close input file Daniel@0: if fclose(fid) < 0, error(['Cannot close file ' filename]); Daniel@0: else fprintf(2, '\rdata read ok \n'); end Daniel@0: Daniel@0: % set values Daniel@0: data_temp(data_temp == realmax) = NaN; Daniel@0: sData.data = data_temp(1:lnum,:); Daniel@0: sData.labels = labs_temp(1:lnum,:); Daniel@0: sData.comp_names = comp_names; Daniel@0: sData.label_names = label_names; Daniel@0: Daniel@0: return; Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%