annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_write_data.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 som_write_data(sData, filename, missing)
Daniel@0 2
Daniel@0 3 %SOM_WRITE_DATA Writes data structs/matrices to a file in SOM_PAK format.
Daniel@0 4 %
Daniel@0 5 % som_write_data(data,filename,[missing])
Daniel@0 6 %
Daniel@0 7 % som_write_data(sD,'system.data')
Daniel@0 8 % som_write_data(D,'system.data','*')
Daniel@0 9 %
Daniel@0 10 % Input and output arguments ([]'s are optional):
Daniel@0 11 % data (struct) data struct to be written in the file
Daniel@0 12 % (matrix) data matrix
Daniel@0 13 % filename (string) output filename
Daniel@0 14 % [missing] (string) string used to denote missing components (NaNs);
Daniel@0 15 % default is 'NaN'
Daniel@0 16 %
Daniel@0 17 % Note that much of the information in the data struct is lost.
Daniel@0 18 % Typically, when saving data structs into files use the 'save' command.
Daniel@0 19 %
Daniel@0 20 % For more help, try 'type som_write_data' or check out online documentation.
Daniel@0 21 % See also SOM_READ_DATA, SOM_READ_COD, SOM_WRITE_COD.
Daniel@0 22
Daniel@0 23 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 24 %
Daniel@0 25 % som_write_data
Daniel@0 26 %
Daniel@0 27 % PURPOSE
Daniel@0 28 %
Daniel@0 29 % Writes data structs/matrices to a file in SOM_PAK format.
Daniel@0 30 %
Daniel@0 31 % SYNTAX
Daniel@0 32 %
Daniel@0 33 % som_write_data(sD,'filename')
Daniel@0 34 % som_write_data(D,'filename')
Daniel@0 35 % som_write_data(...,'missing')
Daniel@0 36 %
Daniel@0 37 % DESCRIPTION
Daniel@0 38 %
Daniel@0 39 % This function is offered for compatibility with SOM_PAK, a SOM software
Daniel@0 40 % package in C. It writes data structs/matrices to a file in SOM_PAK format.
Daniel@0 41 %
Daniel@0 42 % See SOM_READ_DATA for a decription of the SOM_PAK data file format. Since
Daniel@0 43 % the format does not support information on normalizations, that
Daniel@0 44 % information is lost, as well as the data name. The component names are
Daniel@0 45 % written on a comment line which begins with '#n' and label names on
Daniel@0 46 % comment line which begins with '#l', respectively. Any spaces (' ') in the
Daniel@0 47 % component names and in the label names are replaced with
Daniel@0 48 % underscores ('_').
Daniel@0 49 %
Daniel@0 50 % This function is only offered for compatibility with SOM_PAK. In
Daniel@0 51 % general, when saving data in files, use 'save filename.mat sData'. This is
Daniel@0 52 % faster and retains all information of the data struct.
Daniel@0 53 %
Daniel@0 54 % The string to use for missing values (NaNs) in the written file can
Daniel@0 55 % be given with the last argument. Notice that if you use SOM_PAK to
Daniel@0 56 % read the files, you need to set the SOM_PAK environment variable
Daniel@0 57 % LVQSOM_MASK_STR accordingly, e.g. to 'NaN' if you use the default
Daniel@0 58 % replacement. For more information, see the SOM_PAK instructions.
Daniel@0 59 %
Daniel@0 60 % REQUIRED INPUT ARGUMENTS
Daniel@0 61 %
Daniel@0 62 % data (struct or matrix) data to be written
Daniel@0 63 % filename (string) output filename
Daniel@0 64 %
Daniel@0 65 % OPTIONAL INPUT ARGUMENTS
Daniel@0 66 %
Daniel@0 67 % missing (string) string used to denote missing components (NaNs);
Daniel@0 68 % default is 'NaN'
Daniel@0 69 %
Daniel@0 70 % EXAMPLES
Daniel@0 71 %
Daniel@0 72 % The basic usage is:
Daniel@0 73 % som_write_data(sData,'system.data')
Daniel@0 74 %
Daniel@0 75 % To write a data matrix to a file in plain SOM_PAK format, give a
Daniel@0 76 % data matrix as the first argument:
Daniel@0 77 % som_write_data(D,'system.data')
Daniel@0 78 %
Daniel@0 79 % By default, all NaNs in the data matrix are written as 'NaN':s. The
Daniel@0 80 % third argument can be used to change this:
Daniel@0 81 % som_write_data(sData,'system.data','+')
Daniel@0 82 % som_write_data(sData,'system.data','missing')
Daniel@0 83 %
Daniel@0 84 % SEE ALSO
Daniel@0 85 %
Daniel@0 86 % som_read_data Reads data from an ascii file.
Daniel@0 87 % som_read_cod Read a map from a file in SOM_PAK format.
Daniel@0 88 % som_write_cod Writes data struct into a file in SOM_PAK format.
Daniel@0 89
Daniel@0 90 % Copyright (c) 1997-2000 by the SOM toolbox programming team.
Daniel@0 91 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 92
Daniel@0 93 % Version 1.0beta ecco 131197
Daniel@0 94 % Version 2.0beta ecco 030899 juuso 151199
Daniel@0 95
Daniel@0 96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 97 %% check arguments
Daniel@0 98
Daniel@0 99 error(nargchk(2, 3, nargin)); % check no. of input args is correct
Daniel@0 100
Daniel@0 101 % data
Daniel@0 102 if isstruct(sData)
Daniel@0 103 is_struct = 1;
Daniel@0 104 D = sData.data;
Daniel@0 105 else
Daniel@0 106 is_struct = 0;
Daniel@0 107 D = sData;
Daniel@0 108 end
Daniel@0 109 [samples dim] = size(D);
Daniel@0 110
Daniel@0 111 % missing
Daniel@0 112 if nargin == 2, missing = 'NaN'; end
Daniel@0 113
Daniel@0 114 % open output file
Daniel@0 115 fid = fopen(filename, 'w');
Daniel@0 116 if fid < 0, error(['Cannot open file ' filename]); end
Daniel@0 117
Daniel@0 118 % check version
Daniel@0 119 v = version;
Daniel@0 120 ver_53_or_newer = (str2num(v(1:3)) >= 5.3);
Daniel@0 121
Daniel@0 122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 123 %% write data
Daniel@0 124
Daniel@0 125 % write dimension
Daniel@0 126
Daniel@0 127 fprintf(fid, '%d\n', dim);
Daniel@0 128
Daniel@0 129 % write component names as a SOM_PAK comment line
Daniel@0 130 if is_struct,
Daniel@0 131 fprintf(fid,'#n ');
Daniel@0 132 for i = 1:dim, fprintf(fid, '%s ', strrep(sData.comp_names{i},' ','_')); end
Daniel@0 133 fprintf(fid,'\n');
Daniel@0 134 if ~isempty(sData.label_names)
Daniel@0 135 fprintf(fid,'#l ');
Daniel@0 136 l = length(sData.label_names);
Daniel@0 137 for i = 1:l, fprintf(fid, '%s ', strrep(sData.label_names{i},' ','_')); end
Daniel@0 138 fprintf(fid,'\n');
Daniel@0 139 end
Daniel@0 140 end
Daniel@0 141
Daniel@0 142 % are there NaNs and/or labels?
Daniel@0 143
Daniel@0 144 has_nans = isnan(sum(D,2)) * (~strcmp(missing, 'NaN'));
Daniel@0 145
Daniel@0 146 has_labels = 0;
Daniel@0 147 if is_struct
Daniel@0 148 [lines numlabs] = size(sData.labels);
Daniel@0 149 has_labels = zeros(lines, 1);
Daniel@0 150 if ver_53_or_newer
Daniel@0 151 has_labels = sum((~(cellfun('isempty', sData.labels))), 2);
Daniel@0 152 else
Daniel@0 153 for i = 1:lines
Daniel@0 154 for j = 1:numlabs
Daniel@0 155 if ~isempty(sData.labels{i,j})
Daniel@0 156 has_labels(i) = 1; break;
Daniel@0 157 end
Daniel@0 158 end
Daniel@0 159 end
Daniel@0 160 end
Daniel@0 161 end
Daniel@0 162
Daniel@0 163 % write data
Daniel@0 164
Daniel@0 165 form = [repmat('%g ',[1 dim-1]) '%g\n'];
Daniel@0 166
Daniel@0 167 if ~sum(has_labels) & ~sum(has_nans) % no NaNs, no labels
Daniel@0 168 fprintf(fid, form, D');
Daniel@0 169 elseif ~sum(has_labels) % no labels, NaNs
Daniel@0 170 fprintf(fid, '%s', strrep(sprintf(form, D'), 'NaN', missing));
Daniel@0 171 else % labels and NaNs
Daniel@0 172 for i = 1:samples
Daniel@0 173 if has_nans(i)
Daniel@0 174 fprintf(fid, strrep(sprintf('%g ', D(i,:)), 'NaN', missing));
Daniel@0 175 else
Daniel@0 176 fprintf(fid, '%g ', D(i,:));
Daniel@0 177 end
Daniel@0 178
Daniel@0 179 if has_labels(i)
Daniel@0 180 temp = '';
Daniel@0 181 if ver_53_or_newer
Daniel@0 182 nonempty = ~(cellfun('isempty', sData.labels(i,:)));
Daniel@0 183 else
Daniel@0 184 for j = 1:numlabs, nonempty(j) = ~isempty(sData.labels{i, j}); end
Daniel@0 185 end
Daniel@0 186 labs = char(sData.labels{i, nonempty});
Daniel@0 187 labs(:,end + 1) = ' ';
Daniel@0 188 temp = reshape(labs',[1 prod(size(labs))]);
Daniel@0 189 temp(findstr(' ', temp))='';
Daniel@0 190 fprintf(fid, '%s', temp(1:end-1));
Daniel@0 191 end
Daniel@0 192 fprintf(fid,'\n');
Daniel@0 193 end
Daniel@0 194 end
Daniel@0 195
Daniel@0 196 % close file
Daniel@0 197
Daniel@0 198 if fclose(fid),
Daniel@0 199 error(['Cannot close file ' filename]);
Daniel@0 200 else
Daniel@0 201 fprintf(2, 'data write ok\n');
Daniel@0 202 end
Daniel@0 203
Daniel@0 204 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 205
Daniel@0 206
Daniel@0 207
Daniel@0 208
Daniel@0 209
Daniel@0 210