Daniel@0: function som_write_data(sData, filename, missing) Daniel@0: Daniel@0: %SOM_WRITE_DATA Writes data structs/matrices to a file in SOM_PAK format. Daniel@0: % Daniel@0: % som_write_data(data,filename,[missing]) Daniel@0: % Daniel@0: % som_write_data(sD,'system.data') Daniel@0: % som_write_data(D,'system.data','*') Daniel@0: % Daniel@0: % Input and output arguments ([]'s are optional): Daniel@0: % data (struct) data struct to be written in the file Daniel@0: % (matrix) data matrix Daniel@0: % filename (string) output filename Daniel@0: % [missing] (string) string used to denote missing components (NaNs); Daniel@0: % default is 'NaN' Daniel@0: % Daniel@0: % Note that much of the information in the data struct is lost. Daniel@0: % Typically, when saving data structs into files use the 'save' command. Daniel@0: % Daniel@0: % For more help, try 'type som_write_data' or check out online documentation. Daniel@0: % See also SOM_READ_DATA, SOM_READ_COD, SOM_WRITE_COD. Daniel@0: Daniel@0: %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: % Daniel@0: % som_write_data Daniel@0: % Daniel@0: % PURPOSE Daniel@0: % Daniel@0: % Writes data structs/matrices to a file in SOM_PAK format. Daniel@0: % Daniel@0: % SYNTAX Daniel@0: % Daniel@0: % som_write_data(sD,'filename') Daniel@0: % som_write_data(D,'filename') Daniel@0: % som_write_data(...,'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 writes data structs/matrices to a file in SOM_PAK format. Daniel@0: % Daniel@0: % See SOM_READ_DATA for a decription of the SOM_PAK data file format. Since Daniel@0: % the format does not support information on normalizations, that Daniel@0: % information is lost, as well as the data name. The component names are Daniel@0: % written on a comment line which begins with '#n' and label names on Daniel@0: % comment line which begins with '#l', respectively. Any spaces (' ') in the Daniel@0: % component names and in the label names are replaced with Daniel@0: % underscores ('_'). Daniel@0: % Daniel@0: % This function is only offered for compatibility with SOM_PAK. In Daniel@0: % general, when saving data in files, use 'save filename.mat sData'. This is Daniel@0: % faster and retains all information of the data struct. Daniel@0: % Daniel@0: % The string to use for missing values (NaNs) in the written file can Daniel@0: % be given with the last argument. Notice that if you use SOM_PAK to Daniel@0: % read the files, you need to set the SOM_PAK environment variable Daniel@0: % LVQSOM_MASK_STR accordingly, e.g. to 'NaN' if you use the default Daniel@0: % replacement. For more information, see the SOM_PAK instructions. Daniel@0: % Daniel@0: % REQUIRED INPUT ARGUMENTS Daniel@0: % Daniel@0: % data (struct or matrix) data to be written Daniel@0: % filename (string) output filename Daniel@0: % Daniel@0: % OPTIONAL INPUT ARGUMENTS Daniel@0: % Daniel@0: % missing (string) string used to denote missing components (NaNs); Daniel@0: % default is 'NaN' Daniel@0: % Daniel@0: % EXAMPLES Daniel@0: % Daniel@0: % The basic usage is: Daniel@0: % som_write_data(sData,'system.data') Daniel@0: % Daniel@0: % To write a data matrix to a file in plain SOM_PAK format, give a Daniel@0: % data matrix as the first argument: Daniel@0: % som_write_data(D,'system.data') Daniel@0: % Daniel@0: % By default, all NaNs in the data matrix are written as 'NaN':s. The Daniel@0: % third argument can be used to change this: Daniel@0: % som_write_data(sData,'system.data','+') Daniel@0: % som_write_data(sData,'system.data','missing') Daniel@0: % Daniel@0: % SEE ALSO Daniel@0: % Daniel@0: % som_read_data Reads data from an ascii file. 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: 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 131197 Daniel@0: % Version 2.0beta ecco 030899 juuso 151199 Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% check arguments Daniel@0: Daniel@0: error(nargchk(2, 3, nargin)); % check no. of input args is correct Daniel@0: Daniel@0: % data Daniel@0: if isstruct(sData) Daniel@0: is_struct = 1; Daniel@0: D = sData.data; Daniel@0: else Daniel@0: is_struct = 0; Daniel@0: D = sData; Daniel@0: end Daniel@0: [samples dim] = size(D); Daniel@0: Daniel@0: % missing Daniel@0: if nargin == 2, missing = 'NaN'; end Daniel@0: Daniel@0: % open output file Daniel@0: fid = fopen(filename, 'w'); Daniel@0: if fid < 0, error(['Cannot open file ' filename]); end Daniel@0: Daniel@0: % check version Daniel@0: v = version; Daniel@0: ver_53_or_newer = (str2num(v(1:3)) >= 5.3); Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% write data Daniel@0: Daniel@0: % write dimension Daniel@0: Daniel@0: fprintf(fid, '%d\n', dim); Daniel@0: Daniel@0: % write component names as a SOM_PAK comment line Daniel@0: if is_struct, Daniel@0: fprintf(fid,'#n '); Daniel@0: for i = 1:dim, fprintf(fid, '%s ', strrep(sData.comp_names{i},' ','_')); end Daniel@0: fprintf(fid,'\n'); Daniel@0: if ~isempty(sData.label_names) Daniel@0: fprintf(fid,'#l '); Daniel@0: l = length(sData.label_names); Daniel@0: for i = 1:l, fprintf(fid, '%s ', strrep(sData.label_names{i},' ','_')); end Daniel@0: fprintf(fid,'\n'); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % are there NaNs and/or labels? Daniel@0: Daniel@0: has_nans = isnan(sum(D,2)) * (~strcmp(missing, 'NaN')); Daniel@0: Daniel@0: has_labels = 0; Daniel@0: if is_struct Daniel@0: [lines numlabs] = size(sData.labels); Daniel@0: has_labels = zeros(lines, 1); Daniel@0: if ver_53_or_newer Daniel@0: has_labels = sum((~(cellfun('isempty', sData.labels))), 2); Daniel@0: else Daniel@0: for i = 1:lines Daniel@0: for j = 1:numlabs Daniel@0: if ~isempty(sData.labels{i,j}) Daniel@0: has_labels(i) = 1; break; Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % write data Daniel@0: Daniel@0: form = [repmat('%g ',[1 dim-1]) '%g\n']; Daniel@0: Daniel@0: if ~sum(has_labels) & ~sum(has_nans) % no NaNs, no labels Daniel@0: fprintf(fid, form, D'); Daniel@0: elseif ~sum(has_labels) % no labels, NaNs Daniel@0: fprintf(fid, '%s', strrep(sprintf(form, D'), 'NaN', missing)); Daniel@0: else % labels and NaNs Daniel@0: for i = 1:samples Daniel@0: if has_nans(i) Daniel@0: fprintf(fid, strrep(sprintf('%g ', D(i,:)), 'NaN', missing)); Daniel@0: else Daniel@0: fprintf(fid, '%g ', D(i,:)); Daniel@0: end Daniel@0: Daniel@0: if has_labels(i) Daniel@0: temp = ''; Daniel@0: if ver_53_or_newer Daniel@0: nonempty = ~(cellfun('isempty', sData.labels(i,:))); Daniel@0: else Daniel@0: for j = 1:numlabs, nonempty(j) = ~isempty(sData.labels{i, j}); end Daniel@0: end Daniel@0: labs = char(sData.labels{i, nonempty}); Daniel@0: labs(:,end + 1) = ' '; Daniel@0: temp = reshape(labs',[1 prod(size(labs))]); Daniel@0: temp(findstr(' ', temp))=''; Daniel@0: fprintf(fid, '%s', temp(1:end-1)); Daniel@0: end Daniel@0: fprintf(fid,'\n'); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % close file Daniel@0: Daniel@0: if fclose(fid), Daniel@0: error(['Cannot close file ' filename]); Daniel@0: else Daniel@0: fprintf(2, 'data write ok\n'); Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: