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