annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_set.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 [sS, ok, msgs] = som_set(sS, varargin)
Daniel@0 2
Daniel@0 3 %SOM_SET Create and check SOM Toolbox structs, give values to their fields.
Daniel@0 4 %
Daniel@0 5 % [sS, ok, msgs] = som_set(sS, [field, contents, ...])
Daniel@0 6 %
Daniel@0 7 % sM = som_set(sM,'name','SOM#1.1');
Daniel@0 8 % [dummy,ok,msgs] = som_set(sData);
Daniel@0 9 % sT = som_set('som_topol','msize',[10 10],'lattice','hexa');
Daniel@0 10 % [sTrain,ok] = som_set(sTrain,'algorithm','lininit');
Daniel@0 11 % [sN,ok,msgs] = som_set('som_norm');
Daniel@0 12 %
Daniel@0 13 % Input and output arguments ([]'s are optional):
Daniel@0 14 % sS the target struct
Daniel@0 15 % (struct) a SOM Toolbox structure (not visualization struct)
Daniel@0 16 % (string) structure identifier (see below)
Daniel@0 17 % the updated/created structure is returned
Daniel@0 18 % [field, (string) field to be given value to (see below)
Daniel@0 19 % contents] (varies) the contents for the field
Daniel@0 20 %
Daniel@0 21 % ok (vector) status for each field-contents pair (1=ok)
Daniel@0 22 % msgs (cellstr) status string for each field-contents pair (''=ok)
Daniel@0 23 %
Daniel@0 24 % There can be arbitrarily many field-contents pairs. If there
Daniel@0 25 % are _no_ field-content pairs, and the first argument is a struct,
Daniel@0 26 % the fields of the struct are checked for validity.
Daniel@0 27 %
Daniel@0 28 % Valid struct and corresponding field identifiers:
Daniel@0 29 % 'som_map' : 'codebook', 'labels', 'mask', 'neigh', 'name',
Daniel@0 30 % 'topol', 'msize, 'lattice', 'shape',
Daniel@0 31 % 'trainhist', 'comp_names', 'comp_norm',
Daniel@0 32 % 'som_data' : 'data', 'labels', 'name', 'comp_names', 'comp_norm',
Daniel@0 33 % 'label_names'
Daniel@0 34 % 'som_topol' : 'msize', 'lattice', 'shape'
Daniel@0 35 % 'som_norm' : 'method', 'params', 'status'
Daniel@0 36 % 'som_train' : 'algorithm', 'data_name', 'mask', 'neigh',
Daniel@0 37 % 'radius_ini', 'radius_fin', 'alpha_ini', 'alpha_type',
Daniel@0 38 % 'trainlen', 'time'
Daniel@0 39 % 'som_grid' : 'lattice', 'shape', 'msize', 'coord',
Daniel@0 40 % 'line', 'linecolor', 'linewidth',
Daniel@0 41 % 'marker', 'markersize', 'markercolor', 'surf',
Daniel@0 42 % 'label', 'labelcolor', 'labelsize'
Daniel@0 43 % checking given values has not been implemented yet!
Daniel@0 44 %
Daniel@0 45 % For more help, try 'type som_set' or check out online documentation.
Daniel@0 46 % See also SOM_INFO, SOM_MAP_STRUCT, SOM_DATA_STRUCT, SOM_VS1TO2.
Daniel@0 47
Daniel@0 48 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 49 %
Daniel@0 50 % som_set
Daniel@0 51 %
Daniel@0 52 % PURPOSE
Daniel@0 53 %
Daniel@0 54 % Create and set values for fields of SOM Toolbox structs (except
Daniel@0 55 % visualization struct). Can also be used to check the validity of structs.
Daniel@0 56 %
Daniel@0 57 % SYNTAX
Daniel@0 58 %
Daniel@0 59 % sMap = som_set('som_map');
Daniel@0 60 % sData = som_set(sData);
Daniel@0 61 % sNorm = som_set(...,'field',contents,...);
Daniel@0 62 % [sTopol,ok] = som_set(sTopol,...);
Daniel@0 63 % [sTrain,ok,msgs] = som_set('som_train',...);
Daniel@0 64 %
Daniel@0 65 % DESCRIPTION
Daniel@0 66 %
Daniel@0 67 % The function is used to create and set values for fields of SOM
Daniel@0 68 % Toolbox structs, except visualization structs. The given values are
Daniel@0 69 % first checked for validity, and if they are not valid, an error
Daniel@0 70 % message is returned. The function can also be used to check the
Daniel@0 71 % validity of all the fields of the struct by supplying a struct as
Daniel@0 72 % the first and only argument.
Daniel@0 73 %
Daniel@0 74 % NOTE: Using SOM_SET to create structures does _not_ guarantee that the
Daniel@0 75 % structs are valid (try e.g. sM = som_set('som_map'); som_set(sM)). The
Daniel@0 76 % initial values that the function gives to the fields of the structs are
Daniel@0 77 % typically invalid. It is recommended that when creating map or data
Daniel@0 78 % structs, the corresponding functions SOM_MAP_STRUCT and SOM_DATA_STRUCT
Daniel@0 79 % are used instead of SOM_SET. However, when giving values for the fields,
Daniel@0 80 % SOM_SET tries to guarantee that the values are valid.
Daniel@0 81 %
Daniel@0 82 % If a string is given as the first argument, the corresponding
Daniel@0 83 % structure is first created and the field-content pairs are then
Daniel@0 84 % applied to it.
Daniel@0 85 %
Daniel@0 86 % There can be arbitrarily many field-contents pairs. The pairs
Daniel@0 87 % are processed sequentially one pair at a time. For each pair,
Daniel@0 88 % the validity of the contents is checked and the corresponding
Daniel@0 89 % items in the returned 'ok'-vector and 'msgs'-cellstring are set.
Daniel@0 90 % - if the contents is ok, the status is set to 1 and message to ''
Daniel@0 91 % - if the contents is suspicious, status is set to 1, but a
Daniel@0 92 % message is produced
Daniel@0 93 % - if the contents is invalid, status is set to 0 and an error
Daniel@0 94 % message is produced. The contents are _not_ given to the field.
Daniel@0 95 % If there is only one output argument, the status and messages
Daniel@0 96 % for each pair are printed to standard output.
Daniel@0 97 %
Daniel@0 98 % The different field-contents pairs have no effect on each other.
Daniel@0 99 % If a field is given a value multiple times, the last valid one
Daniel@0 100 % stays in effect.
Daniel@0 101 %
Daniel@0 102 % In some cases, the order of the given fields is significant.
Daniel@0 103 % For example in the case of 'som_map', the validity of some fields,
Daniel@0 104 % like '.comp_names', depends on the input space dimension, which is
Daniel@0 105 % checked from the '.data' field (dim = size(sD.data,2) to be specific).
Daniel@0 106 % Therefore, the '.data' field (or '.codebook' field in case of map
Daniel@0 107 % struct) should always be given a value first. Below is a list of
Daniel@0 108 % this kind of dependancies:
Daniel@0 109 %
Daniel@0 110 % som_map: 'comp_names', 'comp_norm', 'msize', 'topol.msize',
Daniel@0 111 % 'labels' and 'mask' depend on 'codebook'
Daniel@0 112 % new value for 'codebook' should have equal size to the old
Daniel@0 113 % one (unless the old one was empty)
Daniel@0 114 % som_data: 'comp_names' and 'comp_norm' depend on 'data'
Daniel@0 115 % new value for 'data' should have equal dimension (size(data,2))
Daniel@0 116 % as the old one (unless the old one was empty)
Daniel@0 117 %
Daniel@0 118 % KNOWN BUGS
Daniel@0 119 %
Daniel@0 120 % Checking the values given to som_grid struct has not been
Daniel@0 121 % implemented. Use SOM_GRID function to give the values.
Daniel@0 122 %
Daniel@0 123 % REQUIRED INPUT ARGUMENTS
Daniel@0 124 %
Daniel@0 125 % sS The struct.
Daniel@0 126 % (struct) A SOM Toolbox struct.
Daniel@0 127 % (string) Identifier of a SOM Toolbox struct: 'som_map',
Daniel@0 128 % 'som_data', 'som_topol', 'som_norm' or 'som_train'
Daniel@0 129 %
Daniel@0 130 % OPTIONAL INPUT ARGUMENTS
Daniel@0 131 %
Daniel@0 132 % field (string) Field identifier string (see below).
Daniel@0 133 % contents (varies) Value for the field (see below).
Daniel@0 134 %
Daniel@0 135 % Below is the list of valid field identifiers for the different
Daniel@0 136 % SOM Toolbox structs.
Daniel@0 137 %
Daniel@0 138 % 'som_map' (map struct)
Daniel@0 139 % 'codebook' : matrix, size [munits, dim]
Daniel@0 140 % 'labels' : cell array of strings,
Daniel@0 141 % size [munits, maximum_number_of_labels]
Daniel@0 142 % 'topol' : topology struct (prod(topol.msize)=munits)
Daniel@0 143 % 'mask' : vector, size [dim, 1]
Daniel@0 144 % 'neigh' : string ('gaussian' or 'cutgauss' or 'bubble' or 'ep')
Daniel@0 145 % 'trainhist' : struct array of train structs
Daniel@0 146 % 'name' : string
Daniel@0 147 % 'comp_names' : cellstr, size [dim, 1], e.g. {'c1','c2','c3'}
Daniel@0 148 % 'comp_norm' : cell array, size [dim, 1], of cell arrays
Daniel@0 149 % of normalization structs
Daniel@0 150 % Also the following can be used (although they are fields
Daniel@0 151 % of the topology struct)
Daniel@0 152 % 'msize' : vector (prod(msize)=munits)
Daniel@0 153 % 'lattice' : string ('rect' or 'hexa')
Daniel@0 154 % 'shape' : string ('sheet' or 'cyl' or 'toroid')
Daniel@0 155 %
Daniel@0 156 % 'som_data' (data struct)
Daniel@0 157 % 'data' : matrix, size [dlen, dim]
Daniel@0 158 % 'name' : string
Daniel@0 159 % 'labels' : cell array of strings,
Daniel@0 160 % size [dlen, m]
Daniel@0 161 % 'comp_names' : cellstr, size [dim, 1], e.g. {'c1','c2','c3'}
Daniel@0 162 % 'comp_norm' : cell array, size [dim, 1], of cell arrays
Daniel@0 163 % of normalization structs
Daniel@0 164 % 'label_names' : cellstr, size [m, 1]
Daniel@0 165 %
Daniel@0 166 % 'som_topol' (topology struct)
Daniel@0 167 % 'msize' : vector
Daniel@0 168 % 'lattice' : string ('rect' or 'hexa')
Daniel@0 169 % 'shape' : string ('sheet' or 'cyl' or 'toroid')
Daniel@0 170 %
Daniel@0 171 % 'som_norm' (normalization struct)
Daniel@0 172 % 'method' : string
Daniel@0 173 % 'params' : varies
Daniel@0 174 % 'status' : string ('done' or 'undone' or 'uninit')
Daniel@0 175 %
Daniel@0 176 % 'som_train' (train struct)
Daniel@0 177 % 'algorithm' : string ('seq' or 'batch' or 'lininit' or 'randinit')
Daniel@0 178 % 'data_name' : string
Daniel@0 179 % 'mask' : vector, size [dim, 1]
Daniel@0 180 % 'neigh' : string ('gaussian' or 'cutgauss' or 'bubble' or 'ep')
Daniel@0 181 % 'radius_ini' : scalar
Daniel@0 182 % 'radius_fin' : scalar
Daniel@0 183 % 'alpha_ini' : scalar
Daniel@0 184 % 'alpha_type' : string ('linear' or 'inv' or 'power')
Daniel@0 185 % 'trainlen' : scalar
Daniel@0 186 % 'time' : string
Daniel@0 187 %
Daniel@0 188 % 'som_grid' (grid struct) : checking the values has not been implemented yet!
Daniel@0 189 % 'lattice' : string ('rect' or 'hexa') or
Daniel@0 190 % (sparce) matrix, size munits x munits
Daniel@0 191 % 'shape' : string ('sheet' or 'cyl' or 'toroid')
Daniel@0 192 % 'msize' : vector, size 1x2
Daniel@0 193 % 'coord' : matrix, size munits x 2 or munits x 3
Daniel@0 194 % 'line' : string (linespec, e.g. '-', or 'none')
Daniel@0 195 % 'linecolor' : RGB triple or string (colorspec, e.g. 'k') or
Daniel@0 196 % munits x munits x 3 (sparce) matrix or cell
Daniel@0 197 % array of RGB triples
Daniel@0 198 % 'linewidth' : scalar or munits x munits (sparce) matrix
Daniel@0 199 % 'marker' : string (markerspec, e.g. 'o', or 'none') or
Daniel@0 200 % munits x 1 cell or char array of these
Daniel@0 201 % 'markersize' : scalar or munits x 1 vector
Daniel@0 202 % 'markercolor' : RGB triple or string (colorspec, e.g. 'k')
Daniel@0 203 % 'surf' : [], munits x 1 or munits x 3 matrix of RGB triples
Daniel@0 204 % 'label' : [] or munits x 1 char array or
Daniel@0 205 % munits x l cell array of strings
Daniel@0 206 % 'labelcolor' : RGB triple or string (colorspec, e.g. 'g' or 'none')
Daniel@0 207 % 'labelsize' : scalar
Daniel@0 208 %
Daniel@0 209 % OUTPUT ARGUMENTS
Daniel@0 210 %
Daniel@0 211 % sS (struct) the created / updated struct
Daniel@0 212 % ok (vector) length = number of field-contents pairs, gives
Daniel@0 213 % validity status for each pair (0=invalid, 1 otherwise)
Daniel@0 214 % msgs (cellstr) length = number of field-contents pairs, gives
Daniel@0 215 % error/warning message for each pair ('' if ok)
Daniel@0 216 %
Daniel@0 217 % EXAMPLES
Daniel@0 218 %
Daniel@0 219 % To create a struct:
Daniel@0 220 % sM = som_set('som_map');
Daniel@0 221 % sD = som_set('som_data');
Daniel@0 222 % sTo = som_set('som_topol');
Daniel@0 223 % sTr = som_set('som_train');
Daniel@0 224 % sN = som_set('som_norm');
Daniel@0 225 % sG = som_set('som_grid');
Daniel@0 226 %
Daniel@0 227 % To check the the contents of a struct:
Daniel@0 228 % som_set(sS);
Daniel@0 229 % [dummy,ok] = som_set(sS);
Daniel@0 230 % [dummy,ok,msgs] = som_set(sS);
Daniel@0 231 %
Daniel@0 232 % To give values to fields:
Daniel@0 233 % sTo = som_set(sTo,'msize',[10 10],'lattice','hexa','shape','toroid');
Daniel@0 234 % sM = som_set('som_map','codebook',rand(100,4),'topol',sTo);
Daniel@0 235 %
Daniel@0 236 % SEE ALSO
Daniel@0 237 %
Daniel@0 238 % som_info Prints information the given struct.
Daniel@0 239 % som_map_struct Create map struct.
Daniel@0 240 % som_data_struct Create data struct.
Daniel@0 241 % som_topol_struct Create topology struct.
Daniel@0 242 % som_train_struct Create training struct.
Daniel@0 243 % som_grid Create and visualize grid struct.
Daniel@0 244 % som_vs1to2 Conversion from version 1.0 structs to 2.0.
Daniel@0 245 % som_vs2to1 Conversion from version 2.0 structs to 1.0.
Daniel@0 246
Daniel@0 247 % Copyright (c) 1999-2000 by the SOM toolbox programming team.
Daniel@0 248 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 249
Daniel@0 250 % Version 2.0beta juuso 101199 130300
Daniel@0 251
Daniel@0 252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 253 %% create struct if necessary
Daniel@0 254
Daniel@0 255 if ischar(sS),
Daniel@0 256 switch sS
Daniel@0 257 case 'som_map',
Daniel@0 258 sS=struct('type', 'som_map', ...
Daniel@0 259 'codebook', [], ...
Daniel@0 260 'topol', som_set('som_topol'), ...
Daniel@0 261 'labels', cell(1), ...
Daniel@0 262 'neigh', 'gaussian', ...
Daniel@0 263 'mask', [], ...
Daniel@0 264 'trainhist', cell(1), ...
Daniel@0 265 'name', '',...
Daniel@0 266 'comp_names', {''}, ...
Daniel@0 267 'comp_norm', cell(1));
Daniel@0 268 case 'som_data',
Daniel@0 269 sS=struct('type', 'som_data', ...
Daniel@0 270 'data', [], ...
Daniel@0 271 'labels', cell(1), ...
Daniel@0 272 'name', '', ...
Daniel@0 273 'comp_names', {''}, ...
Daniel@0 274 'comp_norm', cell(1), ...
Daniel@0 275 'label_names', []);
Daniel@0 276 case 'som_topol',
Daniel@0 277 sS=struct('type', 'som_topol', ...
Daniel@0 278 'msize', 0, ...
Daniel@0 279 'lattice', 'hexa', ...
Daniel@0 280 'shape', 'sheet');
Daniel@0 281 case 'som_train',
Daniel@0 282 sS=struct('type', 'som_train', ...
Daniel@0 283 'algorithm', '', ...
Daniel@0 284 'data_name', '', ...
Daniel@0 285 'neigh', 'gaussian', ...
Daniel@0 286 'mask', [], ...
Daniel@0 287 'radius_ini', NaN, ...
Daniel@0 288 'radius_fin', NaN, ...
Daniel@0 289 'alpha_ini', NaN, ...
Daniel@0 290 'alpha_type', 'inv', ...
Daniel@0 291 'trainlen', NaN, ...
Daniel@0 292 'time', '');
Daniel@0 293 case 'som_norm',
Daniel@0 294 sS=struct('type', 'som_norm', ...
Daniel@0 295 'method', 'var', ...
Daniel@0 296 'params', [], ...
Daniel@0 297 'status', 'uninit');
Daniel@0 298 case 'som_grid',
Daniel@0 299 sS=struct('type','som_grid',...
Daniel@0 300 'lattice','hexa',...
Daniel@0 301 'shape','sheet',...
Daniel@0 302 'msize',[1 1],...
Daniel@0 303 'coord',[],...
Daniel@0 304 'line','-',...
Daniel@0 305 'linecolor',[.9 .9 .9],...
Daniel@0 306 'linewidth',0.5,...
Daniel@0 307 'marker','o',...
Daniel@0 308 'markersize',6,...
Daniel@0 309 'markercolor','k',...
Daniel@0 310 'surf',[],...
Daniel@0 311 'label',[],...
Daniel@0 312 'labelcolor','g',...
Daniel@0 313 'labelsize',12);
Daniel@0 314 otherwise
Daniel@0 315 ok=0; msgs = {['Unrecognized struct type: ' sS]}; sS = [];
Daniel@0 316 return;
Daniel@0 317 end
Daniel@0 318
Daniel@0 319 elseif isstruct(sS) & length(varargin)==0,
Daniel@0 320
Daniel@0 321 % check all fields
Daniel@0 322 fields = fieldnames(sS);
Daniel@0 323 if ~any(strcmp('type',fields)),
Daniel@0 324 error('The struct has no ''type'' field.');
Daniel@0 325 end
Daniel@0 326 k = 0;
Daniel@0 327 for i=1:length(fields),
Daniel@0 328 contents = getfield(sS,fields{i});
Daniel@0 329 if ~strcmp(fields{i},'type'),
Daniel@0 330 varargin{k+1} = fields{i};
Daniel@0 331 varargin{k+2} = contents;
Daniel@0 332 k = k + 2;
Daniel@0 333 else
Daniel@0 334 if ~any(strcmp(contents, ...
Daniel@0 335 {'som_map','som_data','som_topol','som_train','som_norm'})),
Daniel@0 336 error(['Unknown struct type: ' contents]);
Daniel@0 337 end
Daniel@0 338 end
Daniel@0 339 end
Daniel@0 340
Daniel@0 341 end
Daniel@0 342
Daniel@0 343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 344 %% set field values
Daniel@0 345
Daniel@0 346 p = ceil(length(varargin)/2);
Daniel@0 347 ok = ones(p,1);
Daniel@0 348 msgs = cell(p,1);
Daniel@0 349
Daniel@0 350 for i=1:p,
Daniel@0 351 field = varargin{2*i-1};
Daniel@0 352 content = varargin{2*i};
Daniel@0 353 msg = '';
Daniel@0 354 isok = 0;
Daniel@0 355
Daniel@0 356 si = size(content);
Daniel@0 357 isscalar = (prod(si)==1);
Daniel@0 358 isvector = (sum(si>1)==1);
Daniel@0 359 isrowvector = (isvector & si(1)==1);
Daniel@0 360 if isnumeric(content),
Daniel@0 361 iscomplete = all(~isnan(content(:)));
Daniel@0 362 ispositive = all(content(:)>0);
Daniel@0 363 isinteger = all(content(:)==ceil(content(:)));
Daniel@0 364 isrgb = all(content(:)>=0 & content(:)<=1) & size(content,2)==3;
Daniel@0 365 end
Daniel@0 366
Daniel@0 367 switch sS.type,
Daniel@0 368 case 'som_map',
Daniel@0 369 [munits dim] = size(sS.codebook);
Daniel@0 370 switch field,
Daniel@0 371 case 'codebook',
Daniel@0 372 if ~isnumeric(content),
Daniel@0 373 msg = '''codebook'' should be a numeric matrix';
Daniel@0 374 elseif size(content) ~= size(sS.codebook) & ~isempty(sS.codebook),
Daniel@0 375 msg = 'New ''codebook'' must be equal in size to the old one.';
Daniel@0 376 elseif ~iscomplete,
Daniel@0 377 msg = 'Map codebook must not contain NaN''s.';
Daniel@0 378 else
Daniel@0 379 sS.codebook = content; isok=1;
Daniel@0 380 end
Daniel@0 381 case 'labels',
Daniel@0 382 if isempty(content),
Daniel@0 383 sS.labels = cell(munits,1); isok = 1;
Daniel@0 384 elseif size(content,1) ~= munits,
Daniel@0 385 msg = 'Length of labels array must be equal to the number of map units.';
Daniel@0 386 elseif ~iscell(content) & ~ischar(content),
Daniel@0 387 msg = '''labels'' must be a string array or a cell array/matrix.';
Daniel@0 388 else
Daniel@0 389 isok = 1;
Daniel@0 390 if ischar(content), content = cellstr(content);
Daniel@0 391 elseif ~iscellstr(content),
Daniel@0 392 l = prod(size(content));
Daniel@0 393 for j=1:l,
Daniel@0 394 if ischar(content{j}),
Daniel@0 395 if ~isempty(content{j}),
Daniel@0 396 msg = 'Invalid ''labels'' array.';
Daniel@0 397 isok = 0;
Daniel@0 398 break;
Daniel@0 399 else
Daniel@0 400 content{j} = '';
Daniel@0 401 end
Daniel@0 402 end
Daniel@0 403 end
Daniel@0 404 end
Daniel@0 405 if isok, sS.labels = content; end
Daniel@0 406 end
Daniel@0 407 case 'topol',
Daniel@0 408 if ~isstruct(content),
Daniel@0 409 msg = '''topol'' should be a topology struct.';
Daniel@0 410 elseif ~isfield(content,'msize') | ...
Daniel@0 411 ~isfield(content,'lattice') | ...
Daniel@0 412 ~isfield(content,'shape'),
Daniel@0 413 msg = '''topol'' is not a valid topology struct.';
Daniel@0 414 elseif prod(content.msize) ~= munits,
Daniel@0 415 msg = '''topol''.msize does not match the number of map units.';
Daniel@0 416 else
Daniel@0 417 sS.topol = content; isok = 1;
Daniel@0 418 end
Daniel@0 419 case 'msize',
Daniel@0 420 if ~isnumeric(content) | ~isvector | ~ispositive | ~isinteger,
Daniel@0 421 msg = '''msize'' should be a vector with positive integer elements.';
Daniel@0 422 elseif prod(content) ~= munits,
Daniel@0 423 msg = '''msize'' does not match the map size.';
Daniel@0 424 else
Daniel@0 425 sS.topol.msize = content; isok = 1;
Daniel@0 426 end
Daniel@0 427 case 'lattice',
Daniel@0 428 if ~ischar(content),
Daniel@0 429 msg = '''lattice'' should be a string';
Daniel@0 430 elseif ~strcmp(content,'rect') & ~strcmp(content,'hexa'),
Daniel@0 431 msg = ['Unknown lattice type: ' content];
Daniel@0 432 sS.topol.lattice = content; isok = 1;
Daniel@0 433 else
Daniel@0 434 sS.topol.lattice = content; isok = 1;
Daniel@0 435 end
Daniel@0 436 case 'shape',
Daniel@0 437 if ~ischar(content),
Daniel@0 438 msg = '''shape'' should be a string';
Daniel@0 439 elseif ~strcmp(content,'sheet') & ~strcmp(content,'cyl') & ...
Daniel@0 440 ~strcmp(content,'toroid'),
Daniel@0 441 msg = ['Unknown shape type:' content];
Daniel@0 442 sS.topol.shape = content; isok = 1;
Daniel@0 443 else
Daniel@0 444 sS.topol.shape = content; isok = 1;
Daniel@0 445 end
Daniel@0 446 case 'neigh',
Daniel@0 447 if ~ischar(content),
Daniel@0 448 msg = '''neigh'' should be a string';
Daniel@0 449 elseif ~strcmp(content,'gaussian') & ~strcmp(content,'ep') & ...
Daniel@0 450 ~strcmp(content,'cutgauss') & ~strcmp(content,'bubble'),
Daniel@0 451 msg = ['Unknown neighborhood function: ' content];
Daniel@0 452 sS.neigh = content; isok = 1;
Daniel@0 453 else
Daniel@0 454 sS.neigh = content; isok = 1;
Daniel@0 455 end
Daniel@0 456 case 'mask',
Daniel@0 457 if size(content,1) == 1, content = content'; end
Daniel@0 458 if ~isnumeric(content) | size(content) ~= [dim 1],
Daniel@0 459 msg = '''mask'' should be a column vector (size dim x 1).';
Daniel@0 460 else
Daniel@0 461 sS.mask = content; isok = 1;
Daniel@0 462 end
Daniel@0 463 case 'name',
Daniel@0 464 if ~ischar(content),
Daniel@0 465 msg = '''name'' should be a string.';
Daniel@0 466 else
Daniel@0 467 sS.name = content; isok = 1;
Daniel@0 468 end
Daniel@0 469 case 'comp_names',
Daniel@0 470 if ~iscell(content) & ~ischar(content),
Daniel@0 471 msg = '''comp_names'' should be a cell string or a string array.';
Daniel@0 472 elseif length(content) ~= dim,
Daniel@0 473 msg = 'Length of ''comp_names'' should be equal to dim.';
Daniel@0 474 else
Daniel@0 475 if ischar(content), content = cellstr(content); end
Daniel@0 476 if size(content,1)==1, content = content'; end
Daniel@0 477 sS.comp_names = content;
Daniel@0 478 isok = 1;
Daniel@0 479 end
Daniel@0 480 case 'comp_norm',
Daniel@0 481 if ~iscell(content) & length(content)>0,
Daniel@0 482 msg = '''comp_norm'' should be a cell array.';
Daniel@0 483 elseif length(content) ~= dim,
Daniel@0 484 msg = 'Length of ''comp_norm'' should be equal to dim.';
Daniel@0 485 else
Daniel@0 486 isok = 1;
Daniel@0 487 for j=1:length(content),
Daniel@0 488 if ~isempty(content{j}) & (~isfield(content{j}(1),'type') | ...
Daniel@0 489 ~strcmp(content{j}(1).type,'som_norm')),
Daniel@0 490 msg = 'Each cell in ''comp_norm'' should be either empty or type ''som_norm''.';
Daniel@0 491 isok = 0;
Daniel@0 492 break;
Daniel@0 493 end
Daniel@0 494 end
Daniel@0 495 if isok, sS.comp_norm = content; end
Daniel@0 496 end
Daniel@0 497 case 'trainhist',
Daniel@0 498 if ~isstruct(content) & ~isempty(content),
Daniel@0 499 msg = '''trainhist'' should be a struct array or empty.';
Daniel@0 500 else
Daniel@0 501 isok = 1;
Daniel@0 502 for j=1:length(content),
Daniel@0 503 if ~isfield(content(j),'type') | ~strcmp(content(j).type,'som_train'),
Daniel@0 504 msg = 'Each cell in ''trainhist'' should be of type ''som_train''.';
Daniel@0 505 isok = 0;
Daniel@0 506 break;
Daniel@0 507 end
Daniel@0 508 end
Daniel@0 509 if isok, sS.trainhist = content; end
Daniel@0 510 end
Daniel@0 511 otherwise,
Daniel@0 512 msg = ['Invalid field for map struct: ' field];
Daniel@0 513 end
Daniel@0 514
Daniel@0 515 case 'som_data',
Daniel@0 516 [dlen dim] = size(sS.data);
Daniel@0 517 switch field,
Daniel@0 518 case 'data',
Daniel@0 519 [dummy dim2] = size(content);
Daniel@0 520 if prod(si)==0,
Daniel@0 521 msg = '''data'' is empty';
Daniel@0 522 elseif ~isnumeric(content),
Daniel@0 523 msg = '''data'' should be numeric matrix.';
Daniel@0 524 elseif dim ~= dim2 & ~isempty(sS.data),
Daniel@0 525 msg = 'New ''data'' must have the same dimension as old one.';
Daniel@0 526 else
Daniel@0 527 sS.data = content; isok = 1;
Daniel@0 528 end
Daniel@0 529 case 'labels',
Daniel@0 530 if isempty(content),
Daniel@0 531 sS.labels = cell(dlen,1); isok = 1;
Daniel@0 532 elseif size(content,1) ~= dlen,
Daniel@0 533 msg = 'Length of ''labels'' must be equal to the number of data vectors.';
Daniel@0 534 elseif ~iscell(content) & ~ischar(content),
Daniel@0 535 msg = '''labels'' must be a string array or a cell array/matrix.';
Daniel@0 536 else
Daniel@0 537 isok = 1;
Daniel@0 538 if ischar(content), content = cellstr(content);
Daniel@0 539 elseif ~iscellstr(content),
Daniel@0 540 l = prod(size(content));
Daniel@0 541 for j=1:l,
Daniel@0 542 if ~ischar(content{j}),
Daniel@0 543 if ~isempty(content{j}),
Daniel@0 544 msg = 'Invalid ''labels'' array.';
Daniel@0 545 isok = 0; j
Daniel@0 546 break;
Daniel@0 547 else
Daniel@0 548 content{j} = '';
Daniel@0 549 end
Daniel@0 550 end
Daniel@0 551 end
Daniel@0 552 end
Daniel@0 553 if isok, sS.labels = content; end
Daniel@0 554 end
Daniel@0 555 case 'name',
Daniel@0 556 if ~ischar(content),
Daniel@0 557 msg = '''name'' should be a string.';
Daniel@0 558 else
Daniel@0 559 sS.name = content; isok = 1;
Daniel@0 560 end
Daniel@0 561 case 'comp_names',
Daniel@0 562 if ~iscell(content) & ~ischar(content),
Daniel@0 563 msg = '''comp_names'' should be a cell string or a string array.';
Daniel@0 564 elseif length(content) ~= dim,
Daniel@0 565 msg = 'Length of ''comp_names'' should be equal to dim.';
Daniel@0 566 else
Daniel@0 567 if ischar(content), content = cellstr(content); end
Daniel@0 568 if size(content,1)==1, content = content'; end
Daniel@0 569 sS.comp_names = content;
Daniel@0 570 isok = 1;
Daniel@0 571 end
Daniel@0 572 case 'comp_norm',
Daniel@0 573 if ~iscell(content) & length(content)>0,
Daniel@0 574 msg = '''comp_norm'' should be a cell array.';
Daniel@0 575 elseif length(content) ~= dim,
Daniel@0 576 msg = 'Length of ''comp_norm'' should be equal to dim.';
Daniel@0 577 else
Daniel@0 578 isok = 1;
Daniel@0 579 for j=1:length(content),
Daniel@0 580 if ~isempty(content{j}) & (~isfield(content{j}(1),'type') | ...
Daniel@0 581 ~strcmp(content{j}(1).type,'som_norm')),
Daniel@0 582 msg = 'Each cell in ''comp_norm'' should be either empty or type ''som_norm''.';
Daniel@0 583 isok = 0;
Daniel@0 584 break;
Daniel@0 585 end
Daniel@0 586 end
Daniel@0 587 if isok, sS.comp_norm = content; end
Daniel@0 588 end
Daniel@0 589 case 'label_names',
Daniel@0 590 if ~iscell(content) & ~ischar(content) & ~isempty(content),
Daniel@0 591 msg = ['''label_names'' should be a cell string, a string array or' ...
Daniel@0 592 ' empty.'];
Daniel@0 593 else
Daniel@0 594 if ~isempty(content),
Daniel@0 595 if ischar(content), content = cellstr(content); end
Daniel@0 596 if size(content,1)==1, content = content'; end
Daniel@0 597 end
Daniel@0 598 sS.label_names = content;
Daniel@0 599 isok = 1;
Daniel@0 600 end
Daniel@0 601 otherwise,
Daniel@0 602 msg = ['Invalid field for data struct: ' field];
Daniel@0 603 end
Daniel@0 604
Daniel@0 605 case 'som_topol',
Daniel@0 606 switch field,
Daniel@0 607 case 'msize',
Daniel@0 608 if ~isnumeric(content) | ~isvector | ~ispositive | ~isinteger,
Daniel@0 609 msg = '''msize'' should be a vector with positive integer elements.';
Daniel@0 610 else
Daniel@0 611 sS.msize = content; isok=1;
Daniel@0 612 end
Daniel@0 613 case 'lattice',
Daniel@0 614 if ~ischar(content),
Daniel@0 615 msg = '''lattice'' should be a string';
Daniel@0 616 elseif ~strcmp(content,'rect') & ~strcmp(content,'hexa'),
Daniel@0 617 msg = ['Unknown lattice type: ' content];
Daniel@0 618 sS.lattice = content; isok = 1;
Daniel@0 619 else
Daniel@0 620 sS.lattice = content; isok = 1;
Daniel@0 621 end
Daniel@0 622 case 'shape',
Daniel@0 623 if ~ischar(content),
Daniel@0 624 msg = '''shape'' should be a string';
Daniel@0 625 elseif ~strcmp(content,'sheet') & ~strcmp(content,'cyl') & ...
Daniel@0 626 ~strcmp(content,'toroid'),
Daniel@0 627 msg = ['Unknown shape type: ' content];
Daniel@0 628 sS.shape = content; isok = 1;
Daniel@0 629 else
Daniel@0 630 sS.shape = content; isok = 1;
Daniel@0 631 end
Daniel@0 632 otherwise,
Daniel@0 633 msg = ['Invalid field for topology struct: ' field];
Daniel@0 634 end
Daniel@0 635
Daniel@0 636 case 'som_train',
Daniel@0 637 switch field,
Daniel@0 638 case 'algorithm',
Daniel@0 639 if ~ischar(content),
Daniel@0 640 msg = '''algorithm'' should be a string.';
Daniel@0 641 else
Daniel@0 642 sS.algorithm = content; isok = 1;
Daniel@0 643 end
Daniel@0 644 case 'data_name',
Daniel@0 645 if ~ischar(content),
Daniel@0 646 msg = '''data_name'' should be a string';
Daniel@0 647 else
Daniel@0 648 sS.data_name = content; isok = 1;
Daniel@0 649 end
Daniel@0 650 case 'neigh',
Daniel@0 651 if ~ischar(content),
Daniel@0 652 msg = '''neigh'' should be a string';
Daniel@0 653 elseif ~isempty(content) & ~strcmp(content,'gaussian') & ~strcmp(content,'ep') & ...
Daniel@0 654 ~strcmp(content,'cutgauss') & ~strcmp(content,'bubble'),
Daniel@0 655 msg = ['Unknown neighborhood function: ' content];
Daniel@0 656 sS.neigh = content; isok = 1;
Daniel@0 657 else
Daniel@0 658 sS.neigh = content; isok = 1;
Daniel@0 659 end
Daniel@0 660 case 'mask',
Daniel@0 661 if size(content,1) == 1, content = content'; end
Daniel@0 662 dim = size(content,1); %[munits dim] = size(sS.data);
Daniel@0 663 if ~isnumeric(content) | size(content) ~= [dim 1],
Daniel@0 664 msg = '''mask'' should be a column vector (size dim x 1).';
Daniel@0 665 else
Daniel@0 666 sS.mask = content; isok = 1;
Daniel@0 667 end
Daniel@0 668 case 'radius_ini',
Daniel@0 669 if ~isnumeric(content) | ~isscalar,
Daniel@0 670 msg = '''radius_ini'' should be a scalar.';
Daniel@0 671 else
Daniel@0 672 sS.radius_ini = content; isok = 1;
Daniel@0 673 end
Daniel@0 674 case 'radius_fin',
Daniel@0 675 if ~isnumeric(content) | ~isscalar,
Daniel@0 676 msg = '''radius_fin'' should be a scalar.';
Daniel@0 677 else
Daniel@0 678 sS.radius_fin = content; isok = 1;
Daniel@0 679 end
Daniel@0 680 case 'alpha_ini',
Daniel@0 681 if ~isnumeric(content) | ~isscalar,
Daniel@0 682 msg = '''alpha_ini'' should be a scalar.';
Daniel@0 683 else
Daniel@0 684 sS.alpha_ini = content; isok = 1;
Daniel@0 685 end
Daniel@0 686 case 'alpha_type',
Daniel@0 687 if ~ischar(content),
Daniel@0 688 msg = '''alpha_type'' should be a string';
Daniel@0 689 elseif ~strcmp(content,'linear') & ~strcmp(content,'inv') & ...
Daniel@0 690 ~strcmp(content,'power') & ~strcmp(content,'constant') & ~strcmp(content,''),
Daniel@0 691 msg = ['Unknown alpha type: ' content];
Daniel@0 692 sS.alpha_type = content; isok = 1;
Daniel@0 693 else
Daniel@0 694 sS.alpha_type = content; isok = 1;
Daniel@0 695 end
Daniel@0 696 case 'trainlen',
Daniel@0 697 if ~isnumeric(content) | ~isscalar,
Daniel@0 698 msg = '''trainlen'' should be a scalar.';
Daniel@0 699 else
Daniel@0 700 sS.trainlen = content; isok = 1;
Daniel@0 701 end
Daniel@0 702 case 'time',
Daniel@0 703 if ~ischar(content),
Daniel@0 704 msg = '''time'' should be a string';
Daniel@0 705 else
Daniel@0 706 sS.time = content; isok = 1;
Daniel@0 707 end
Daniel@0 708 otherwise,
Daniel@0 709 msg = ['Invalid field for train struct: ' field];
Daniel@0 710 end
Daniel@0 711
Daniel@0 712 case 'som_norm',
Daniel@0 713 switch field,
Daniel@0 714 case 'method',
Daniel@0 715 if ~ischar(field),
Daniel@0 716 msg = '''method'' should be a string.';
Daniel@0 717 else
Daniel@0 718 sS.method = content; isok = 1;
Daniel@0 719 end
Daniel@0 720 case 'params',
Daniel@0 721 sS.params = content; isok = 1;
Daniel@0 722 case 'status',
Daniel@0 723 if ~ischar(content),
Daniel@0 724 msg = '''status'' should be a string';
Daniel@0 725 elseif ~strcmp(content,'done') & ~strcmp(content,'undone') & ...
Daniel@0 726 ~strcmp(content,'uninit'),
Daniel@0 727 msg = ['Unknown status type: ' content];
Daniel@0 728 sS.status = content; isok = 1;
Daniel@0 729 else
Daniel@0 730 sS.status = content; isok = 1;
Daniel@0 731 end
Daniel@0 732 otherwise,
Daniel@0 733 msg = ['Invalid field for normalization struct: ' field];
Daniel@0 734 end
Daniel@0 735
Daniel@0 736 case 'som_grid',
Daniel@0 737 if any(strcmp(field,{'lattice', 'shape', 'msize', 'coord',...
Daniel@0 738 'line', 'linecolor', 'linewidth', ...
Daniel@0 739 'marker', 'markersize', 'markercolor', 'surf', ...
Daniel@0 740 'label', 'labelcolor', 'labelsize'})),
Daniel@0 741 warning('No checking done on field identifier or content.');
Daniel@0 742 sS = setfield(sS,field,content);
Daniel@0 743 isok = 1;
Daniel@0 744 else
Daniel@0 745 msg = ['Invalid field for grid struct: ' field];
Daniel@0 746 end
Daniel@0 747
Daniel@0 748 otherwise,
Daniel@0 749 error('Unrecognized structure.');
Daniel@0 750
Daniel@0 751 end
Daniel@0 752
Daniel@0 753 msgs{i} = msg;
Daniel@0 754 ok(i) = isok;
Daniel@0 755
Daniel@0 756 end
Daniel@0 757
Daniel@0 758
Daniel@0 759 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 760 %% return
Daniel@0 761
Daniel@0 762 if nargout < 2,
Daniel@0 763 for i=1:p,
Daniel@0 764 if ~isempty(msgs{i}),
Daniel@0 765 if ~ok(i), fprintf(1,'[Error! ');
Daniel@0 766 else fprintf(1,'[Notice ');
Daniel@0 767 end
Daniel@0 768 fprintf(1,'in setting %s] ',varargin{2*i-1});
Daniel@0 769 fprintf(1,'%s\n',msgs{i});
Daniel@0 770 end
Daniel@0 771 end
Daniel@0 772 end
Daniel@0 773
Daniel@0 774 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 775
Daniel@0 776
Daniel@0 777