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