comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_write_data.m @ 0:e9a9cd732c1e tip

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