Daniel@0: function [sS, ok, msgs] = som_set(sS, varargin) Daniel@0: Daniel@0: %SOM_SET Create and check SOM Toolbox structs, give values to their fields. Daniel@0: % Daniel@0: % [sS, ok, msgs] = som_set(sS, [field, contents, ...]) Daniel@0: % Daniel@0: % sM = som_set(sM,'name','SOM#1.1'); Daniel@0: % [dummy,ok,msgs] = som_set(sData); Daniel@0: % sT = som_set('som_topol','msize',[10 10],'lattice','hexa'); Daniel@0: % [sTrain,ok] = som_set(sTrain,'algorithm','lininit'); Daniel@0: % [sN,ok,msgs] = som_set('som_norm'); Daniel@0: % Daniel@0: % Input and output arguments ([]'s are optional): Daniel@0: % sS the target struct Daniel@0: % (struct) a SOM Toolbox structure (not visualization struct) Daniel@0: % (string) structure identifier (see below) Daniel@0: % the updated/created structure is returned Daniel@0: % [field, (string) field to be given value to (see below) Daniel@0: % contents] (varies) the contents for the field Daniel@0: % Daniel@0: % ok (vector) status for each field-contents pair (1=ok) Daniel@0: % msgs (cellstr) status string for each field-contents pair (''=ok) Daniel@0: % Daniel@0: % There can be arbitrarily many field-contents pairs. If there Daniel@0: % are _no_ field-content pairs, and the first argument is a struct, Daniel@0: % the fields of the struct are checked for validity. Daniel@0: % Daniel@0: % Valid struct and corresponding field identifiers: Daniel@0: % 'som_map' : 'codebook', 'labels', 'mask', 'neigh', 'name', Daniel@0: % 'topol', 'msize, 'lattice', 'shape', Daniel@0: % 'trainhist', 'comp_names', 'comp_norm', Daniel@0: % 'som_data' : 'data', 'labels', 'name', 'comp_names', 'comp_norm', Daniel@0: % 'label_names' Daniel@0: % 'som_topol' : 'msize', 'lattice', 'shape' Daniel@0: % 'som_norm' : 'method', 'params', 'status' Daniel@0: % 'som_train' : 'algorithm', 'data_name', 'mask', 'neigh', Daniel@0: % 'radius_ini', 'radius_fin', 'alpha_ini', 'alpha_type', Daniel@0: % 'trainlen', 'time' Daniel@0: % 'som_grid' : 'lattice', 'shape', 'msize', 'coord', Daniel@0: % 'line', 'linecolor', 'linewidth', Daniel@0: % 'marker', 'markersize', 'markercolor', 'surf', Daniel@0: % 'label', 'labelcolor', 'labelsize' Daniel@0: % checking given values has not been implemented yet! Daniel@0: % Daniel@0: % For more help, try 'type som_set' or check out online documentation. Daniel@0: % See also SOM_INFO, SOM_MAP_STRUCT, SOM_DATA_STRUCT, SOM_VS1TO2. Daniel@0: Daniel@0: %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: % Daniel@0: % som_set Daniel@0: % Daniel@0: % PURPOSE Daniel@0: % Daniel@0: % Create and set values for fields of SOM Toolbox structs (except Daniel@0: % visualization struct). Can also be used to check the validity of structs. Daniel@0: % Daniel@0: % SYNTAX Daniel@0: % Daniel@0: % sMap = som_set('som_map'); Daniel@0: % sData = som_set(sData); Daniel@0: % sNorm = som_set(...,'field',contents,...); Daniel@0: % [sTopol,ok] = som_set(sTopol,...); Daniel@0: % [sTrain,ok,msgs] = som_set('som_train',...); Daniel@0: % Daniel@0: % DESCRIPTION Daniel@0: % Daniel@0: % The function is used to create and set values for fields of SOM Daniel@0: % Toolbox structs, except visualization structs. The given values are Daniel@0: % first checked for validity, and if they are not valid, an error Daniel@0: % message is returned. The function can also be used to check the Daniel@0: % validity of all the fields of the struct by supplying a struct as Daniel@0: % the first and only argument. Daniel@0: % Daniel@0: % NOTE: Using SOM_SET to create structures does _not_ guarantee that the Daniel@0: % structs are valid (try e.g. sM = som_set('som_map'); som_set(sM)). The Daniel@0: % initial values that the function gives to the fields of the structs are Daniel@0: % typically invalid. It is recommended that when creating map or data Daniel@0: % structs, the corresponding functions SOM_MAP_STRUCT and SOM_DATA_STRUCT Daniel@0: % are used instead of SOM_SET. However, when giving values for the fields, Daniel@0: % SOM_SET tries to guarantee that the values are valid. Daniel@0: % Daniel@0: % If a string is given as the first argument, the corresponding Daniel@0: % structure is first created and the field-content pairs are then Daniel@0: % applied to it. Daniel@0: % Daniel@0: % There can be arbitrarily many field-contents pairs. The pairs Daniel@0: % are processed sequentially one pair at a time. For each pair, Daniel@0: % the validity of the contents is checked and the corresponding Daniel@0: % items in the returned 'ok'-vector and 'msgs'-cellstring are set. Daniel@0: % - if the contents is ok, the status is set to 1 and message to '' Daniel@0: % - if the contents is suspicious, status is set to 1, but a Daniel@0: % message is produced Daniel@0: % - if the contents is invalid, status is set to 0 and an error Daniel@0: % message is produced. The contents are _not_ given to the field. Daniel@0: % If there is only one output argument, the status and messages Daniel@0: % for each pair are printed to standard output. Daniel@0: % Daniel@0: % The different field-contents pairs have no effect on each other. Daniel@0: % If a field is given a value multiple times, the last valid one Daniel@0: % stays in effect. Daniel@0: % Daniel@0: % In some cases, the order of the given fields is significant. Daniel@0: % For example in the case of 'som_map', the validity of some fields, Daniel@0: % like '.comp_names', depends on the input space dimension, which is Daniel@0: % checked from the '.data' field (dim = size(sD.data,2) to be specific). Daniel@0: % Therefore, the '.data' field (or '.codebook' field in case of map Daniel@0: % struct) should always be given a value first. Below is a list of Daniel@0: % this kind of dependancies: Daniel@0: % Daniel@0: % som_map: 'comp_names', 'comp_norm', 'msize', 'topol.msize', Daniel@0: % 'labels' and 'mask' depend on 'codebook' Daniel@0: % new value for 'codebook' should have equal size to the old Daniel@0: % one (unless the old one was empty) Daniel@0: % som_data: 'comp_names' and 'comp_norm' depend on 'data' Daniel@0: % new value for 'data' should have equal dimension (size(data,2)) Daniel@0: % as the old one (unless the old one was empty) Daniel@0: % Daniel@0: % KNOWN BUGS Daniel@0: % Daniel@0: % Checking the values given to som_grid struct has not been Daniel@0: % implemented. Use SOM_GRID function to give the values. Daniel@0: % Daniel@0: % REQUIRED INPUT ARGUMENTS Daniel@0: % Daniel@0: % sS The struct. Daniel@0: % (struct) A SOM Toolbox struct. Daniel@0: % (string) Identifier of a SOM Toolbox struct: 'som_map', Daniel@0: % 'som_data', 'som_topol', 'som_norm' or 'som_train' Daniel@0: % Daniel@0: % OPTIONAL INPUT ARGUMENTS Daniel@0: % Daniel@0: % field (string) Field identifier string (see below). Daniel@0: % contents (varies) Value for the field (see below). Daniel@0: % Daniel@0: % Below is the list of valid field identifiers for the different Daniel@0: % SOM Toolbox structs. Daniel@0: % Daniel@0: % 'som_map' (map struct) Daniel@0: % 'codebook' : matrix, size [munits, dim] Daniel@0: % 'labels' : cell array of strings, Daniel@0: % size [munits, maximum_number_of_labels] Daniel@0: % 'topol' : topology struct (prod(topol.msize)=munits) Daniel@0: % 'mask' : vector, size [dim, 1] Daniel@0: % 'neigh' : string ('gaussian' or 'cutgauss' or 'bubble' or 'ep') Daniel@0: % 'trainhist' : struct array of train structs Daniel@0: % 'name' : string Daniel@0: % 'comp_names' : cellstr, size [dim, 1], e.g. {'c1','c2','c3'} Daniel@0: % 'comp_norm' : cell array, size [dim, 1], of cell arrays Daniel@0: % of normalization structs Daniel@0: % Also the following can be used (although they are fields Daniel@0: % of the topology struct) Daniel@0: % 'msize' : vector (prod(msize)=munits) Daniel@0: % 'lattice' : string ('rect' or 'hexa') Daniel@0: % 'shape' : string ('sheet' or 'cyl' or 'toroid') Daniel@0: % Daniel@0: % 'som_data' (data struct) Daniel@0: % 'data' : matrix, size [dlen, dim] Daniel@0: % 'name' : string Daniel@0: % 'labels' : cell array of strings, Daniel@0: % size [dlen, m] Daniel@0: % 'comp_names' : cellstr, size [dim, 1], e.g. {'c1','c2','c3'} Daniel@0: % 'comp_norm' : cell array, size [dim, 1], of cell arrays Daniel@0: % of normalization structs Daniel@0: % 'label_names' : cellstr, size [m, 1] Daniel@0: % Daniel@0: % 'som_topol' (topology struct) Daniel@0: % 'msize' : vector Daniel@0: % 'lattice' : string ('rect' or 'hexa') Daniel@0: % 'shape' : string ('sheet' or 'cyl' or 'toroid') Daniel@0: % Daniel@0: % 'som_norm' (normalization struct) Daniel@0: % 'method' : string Daniel@0: % 'params' : varies Daniel@0: % 'status' : string ('done' or 'undone' or 'uninit') Daniel@0: % Daniel@0: % 'som_train' (train struct) Daniel@0: % 'algorithm' : string ('seq' or 'batch' or 'lininit' or 'randinit') Daniel@0: % 'data_name' : string Daniel@0: % 'mask' : vector, size [dim, 1] Daniel@0: % 'neigh' : string ('gaussian' or 'cutgauss' or 'bubble' or 'ep') Daniel@0: % 'radius_ini' : scalar Daniel@0: % 'radius_fin' : scalar Daniel@0: % 'alpha_ini' : scalar Daniel@0: % 'alpha_type' : string ('linear' or 'inv' or 'power') Daniel@0: % 'trainlen' : scalar Daniel@0: % 'time' : string Daniel@0: % Daniel@0: % 'som_grid' (grid struct) : checking the values has not been implemented yet! Daniel@0: % 'lattice' : string ('rect' or 'hexa') or Daniel@0: % (sparce) matrix, size munits x munits Daniel@0: % 'shape' : string ('sheet' or 'cyl' or 'toroid') Daniel@0: % 'msize' : vector, size 1x2 Daniel@0: % 'coord' : matrix, size munits x 2 or munits x 3 Daniel@0: % 'line' : string (linespec, e.g. '-', or 'none') Daniel@0: % 'linecolor' : RGB triple or string (colorspec, e.g. 'k') or Daniel@0: % munits x munits x 3 (sparce) matrix or cell Daniel@0: % array of RGB triples Daniel@0: % 'linewidth' : scalar or munits x munits (sparce) matrix Daniel@0: % 'marker' : string (markerspec, e.g. 'o', or 'none') or Daniel@0: % munits x 1 cell or char array of these Daniel@0: % 'markersize' : scalar or munits x 1 vector Daniel@0: % 'markercolor' : RGB triple or string (colorspec, e.g. 'k') Daniel@0: % 'surf' : [], munits x 1 or munits x 3 matrix of RGB triples Daniel@0: % 'label' : [] or munits x 1 char array or Daniel@0: % munits x l cell array of strings Daniel@0: % 'labelcolor' : RGB triple or string (colorspec, e.g. 'g' or 'none') Daniel@0: % 'labelsize' : scalar Daniel@0: % Daniel@0: % OUTPUT ARGUMENTS Daniel@0: % Daniel@0: % sS (struct) the created / updated struct Daniel@0: % ok (vector) length = number of field-contents pairs, gives Daniel@0: % validity status for each pair (0=invalid, 1 otherwise) Daniel@0: % msgs (cellstr) length = number of field-contents pairs, gives Daniel@0: % error/warning message for each pair ('' if ok) Daniel@0: % Daniel@0: % EXAMPLES Daniel@0: % Daniel@0: % To create a struct: Daniel@0: % sM = som_set('som_map'); Daniel@0: % sD = som_set('som_data'); Daniel@0: % sTo = som_set('som_topol'); Daniel@0: % sTr = som_set('som_train'); Daniel@0: % sN = som_set('som_norm'); Daniel@0: % sG = som_set('som_grid'); Daniel@0: % Daniel@0: % To check the the contents of a struct: Daniel@0: % som_set(sS); Daniel@0: % [dummy,ok] = som_set(sS); Daniel@0: % [dummy,ok,msgs] = som_set(sS); Daniel@0: % Daniel@0: % To give values to fields: Daniel@0: % sTo = som_set(sTo,'msize',[10 10],'lattice','hexa','shape','toroid'); Daniel@0: % sM = som_set('som_map','codebook',rand(100,4),'topol',sTo); Daniel@0: % Daniel@0: % SEE ALSO Daniel@0: % Daniel@0: % som_info Prints information the given struct. Daniel@0: % som_map_struct Create map struct. Daniel@0: % som_data_struct Create data struct. Daniel@0: % som_topol_struct Create topology struct. Daniel@0: % som_train_struct Create training struct. Daniel@0: % som_grid Create and visualize grid struct. Daniel@0: % som_vs1to2 Conversion from version 1.0 structs to 2.0. Daniel@0: % som_vs2to1 Conversion from version 2.0 structs to 1.0. Daniel@0: Daniel@0: % Copyright (c) 1999-2000 by the SOM toolbox programming team. Daniel@0: % http://www.cis.hut.fi/projects/somtoolbox/ Daniel@0: Daniel@0: % Version 2.0beta juuso 101199 130300 Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% create struct if necessary Daniel@0: Daniel@0: if ischar(sS), Daniel@0: switch sS Daniel@0: case 'som_map', Daniel@0: sS=struct('type', 'som_map', ... Daniel@0: 'codebook', [], ... Daniel@0: 'topol', som_set('som_topol'), ... Daniel@0: 'labels', cell(1), ... Daniel@0: 'neigh', 'gaussian', ... Daniel@0: 'mask', [], ... Daniel@0: 'trainhist', cell(1), ... Daniel@0: 'name', '',... Daniel@0: 'comp_names', {''}, ... Daniel@0: 'comp_norm', cell(1)); Daniel@0: case 'som_data', Daniel@0: sS=struct('type', 'som_data', ... Daniel@0: 'data', [], ... Daniel@0: 'labels', cell(1), ... Daniel@0: 'name', '', ... Daniel@0: 'comp_names', {''}, ... Daniel@0: 'comp_norm', cell(1), ... Daniel@0: 'label_names', []); Daniel@0: case 'som_topol', Daniel@0: sS=struct('type', 'som_topol', ... Daniel@0: 'msize', 0, ... Daniel@0: 'lattice', 'hexa', ... Daniel@0: 'shape', 'sheet'); Daniel@0: case 'som_train', Daniel@0: sS=struct('type', 'som_train', ... Daniel@0: 'algorithm', '', ... Daniel@0: 'data_name', '', ... Daniel@0: 'neigh', 'gaussian', ... Daniel@0: 'mask', [], ... Daniel@0: 'radius_ini', NaN, ... Daniel@0: 'radius_fin', NaN, ... Daniel@0: 'alpha_ini', NaN, ... Daniel@0: 'alpha_type', 'inv', ... Daniel@0: 'trainlen', NaN, ... Daniel@0: 'time', ''); Daniel@0: case 'som_norm', Daniel@0: sS=struct('type', 'som_norm', ... Daniel@0: 'method', 'var', ... Daniel@0: 'params', [], ... Daniel@0: 'status', 'uninit'); Daniel@0: case 'som_grid', Daniel@0: sS=struct('type','som_grid',... Daniel@0: 'lattice','hexa',... Daniel@0: 'shape','sheet',... Daniel@0: 'msize',[1 1],... Daniel@0: 'coord',[],... Daniel@0: 'line','-',... Daniel@0: 'linecolor',[.9 .9 .9],... Daniel@0: 'linewidth',0.5,... Daniel@0: 'marker','o',... Daniel@0: 'markersize',6,... Daniel@0: 'markercolor','k',... Daniel@0: 'surf',[],... Daniel@0: 'label',[],... Daniel@0: 'labelcolor','g',... Daniel@0: 'labelsize',12); Daniel@0: otherwise Daniel@0: ok=0; msgs = {['Unrecognized struct type: ' sS]}; sS = []; Daniel@0: return; Daniel@0: end Daniel@0: Daniel@0: elseif isstruct(sS) & length(varargin)==0, Daniel@0: Daniel@0: % check all fields Daniel@0: fields = fieldnames(sS); Daniel@0: if ~any(strcmp('type',fields)), Daniel@0: error('The struct has no ''type'' field.'); Daniel@0: end Daniel@0: k = 0; Daniel@0: for i=1:length(fields), Daniel@0: contents = getfield(sS,fields{i}); Daniel@0: if ~strcmp(fields{i},'type'), Daniel@0: varargin{k+1} = fields{i}; Daniel@0: varargin{k+2} = contents; Daniel@0: k = k + 2; Daniel@0: else Daniel@0: if ~any(strcmp(contents, ... Daniel@0: {'som_map','som_data','som_topol','som_train','som_norm'})), Daniel@0: error(['Unknown struct type: ' contents]); Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% set field values Daniel@0: Daniel@0: p = ceil(length(varargin)/2); Daniel@0: ok = ones(p,1); Daniel@0: msgs = cell(p,1); Daniel@0: Daniel@0: for i=1:p, Daniel@0: field = varargin{2*i-1}; Daniel@0: content = varargin{2*i}; Daniel@0: msg = ''; Daniel@0: isok = 0; Daniel@0: Daniel@0: si = size(content); Daniel@0: isscalar = (prod(si)==1); Daniel@0: isvector = (sum(si>1)==1); Daniel@0: isrowvector = (isvector & si(1)==1); Daniel@0: if isnumeric(content), Daniel@0: iscomplete = all(~isnan(content(:))); Daniel@0: ispositive = all(content(:)>0); Daniel@0: isinteger = all(content(:)==ceil(content(:))); Daniel@0: isrgb = all(content(:)>=0 & content(:)<=1) & size(content,2)==3; Daniel@0: end Daniel@0: Daniel@0: switch sS.type, Daniel@0: case 'som_map', Daniel@0: [munits dim] = size(sS.codebook); Daniel@0: switch field, Daniel@0: case 'codebook', Daniel@0: if ~isnumeric(content), Daniel@0: msg = '''codebook'' should be a numeric matrix'; Daniel@0: elseif size(content) ~= size(sS.codebook) & ~isempty(sS.codebook), Daniel@0: msg = 'New ''codebook'' must be equal in size to the old one.'; Daniel@0: elseif ~iscomplete, Daniel@0: msg = 'Map codebook must not contain NaN''s.'; Daniel@0: else Daniel@0: sS.codebook = content; isok=1; Daniel@0: end Daniel@0: case 'labels', Daniel@0: if isempty(content), Daniel@0: sS.labels = cell(munits,1); isok = 1; Daniel@0: elseif size(content,1) ~= munits, Daniel@0: msg = 'Length of labels array must be equal to the number of map units.'; Daniel@0: elseif ~iscell(content) & ~ischar(content), Daniel@0: msg = '''labels'' must be a string array or a cell array/matrix.'; Daniel@0: else Daniel@0: isok = 1; Daniel@0: if ischar(content), content = cellstr(content); Daniel@0: elseif ~iscellstr(content), Daniel@0: l = prod(size(content)); Daniel@0: for j=1:l, Daniel@0: if ischar(content{j}), Daniel@0: if ~isempty(content{j}), Daniel@0: msg = 'Invalid ''labels'' array.'; Daniel@0: isok = 0; Daniel@0: break; Daniel@0: else Daniel@0: content{j} = ''; Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: if isok, sS.labels = content; end Daniel@0: end Daniel@0: case 'topol', Daniel@0: if ~isstruct(content), Daniel@0: msg = '''topol'' should be a topology struct.'; Daniel@0: elseif ~isfield(content,'msize') | ... Daniel@0: ~isfield(content,'lattice') | ... Daniel@0: ~isfield(content,'shape'), Daniel@0: msg = '''topol'' is not a valid topology struct.'; Daniel@0: elseif prod(content.msize) ~= munits, Daniel@0: msg = '''topol''.msize does not match the number of map units.'; Daniel@0: else Daniel@0: sS.topol = content; isok = 1; Daniel@0: end Daniel@0: case 'msize', Daniel@0: if ~isnumeric(content) | ~isvector | ~ispositive | ~isinteger, Daniel@0: msg = '''msize'' should be a vector with positive integer elements.'; Daniel@0: elseif prod(content) ~= munits, Daniel@0: msg = '''msize'' does not match the map size.'; Daniel@0: else Daniel@0: sS.topol.msize = content; isok = 1; Daniel@0: end Daniel@0: case 'lattice', Daniel@0: if ~ischar(content), Daniel@0: msg = '''lattice'' should be a string'; Daniel@0: elseif ~strcmp(content,'rect') & ~strcmp(content,'hexa'), Daniel@0: msg = ['Unknown lattice type: ' content]; Daniel@0: sS.topol.lattice = content; isok = 1; Daniel@0: else Daniel@0: sS.topol.lattice = content; isok = 1; Daniel@0: end Daniel@0: case 'shape', Daniel@0: if ~ischar(content), Daniel@0: msg = '''shape'' should be a string'; Daniel@0: elseif ~strcmp(content,'sheet') & ~strcmp(content,'cyl') & ... Daniel@0: ~strcmp(content,'toroid'), Daniel@0: msg = ['Unknown shape type:' content]; Daniel@0: sS.topol.shape = content; isok = 1; Daniel@0: else Daniel@0: sS.topol.shape = content; isok = 1; Daniel@0: end Daniel@0: case 'neigh', Daniel@0: if ~ischar(content), Daniel@0: msg = '''neigh'' should be a string'; Daniel@0: elseif ~strcmp(content,'gaussian') & ~strcmp(content,'ep') & ... Daniel@0: ~strcmp(content,'cutgauss') & ~strcmp(content,'bubble'), Daniel@0: msg = ['Unknown neighborhood function: ' content]; Daniel@0: sS.neigh = content; isok = 1; Daniel@0: else Daniel@0: sS.neigh = content; isok = 1; Daniel@0: end Daniel@0: case 'mask', Daniel@0: if size(content,1) == 1, content = content'; end Daniel@0: if ~isnumeric(content) | size(content) ~= [dim 1], Daniel@0: msg = '''mask'' should be a column vector (size dim x 1).'; Daniel@0: else Daniel@0: sS.mask = content; isok = 1; Daniel@0: end Daniel@0: case 'name', Daniel@0: if ~ischar(content), Daniel@0: msg = '''name'' should be a string.'; Daniel@0: else Daniel@0: sS.name = content; isok = 1; Daniel@0: end Daniel@0: case 'comp_names', Daniel@0: if ~iscell(content) & ~ischar(content), Daniel@0: msg = '''comp_names'' should be a cell string or a string array.'; Daniel@0: elseif length(content) ~= dim, Daniel@0: msg = 'Length of ''comp_names'' should be equal to dim.'; Daniel@0: else Daniel@0: if ischar(content), content = cellstr(content); end Daniel@0: if size(content,1)==1, content = content'; end Daniel@0: sS.comp_names = content; Daniel@0: isok = 1; Daniel@0: end Daniel@0: case 'comp_norm', Daniel@0: if ~iscell(content) & length(content)>0, Daniel@0: msg = '''comp_norm'' should be a cell array.'; Daniel@0: elseif length(content) ~= dim, Daniel@0: msg = 'Length of ''comp_norm'' should be equal to dim.'; Daniel@0: else Daniel@0: isok = 1; Daniel@0: for j=1:length(content), Daniel@0: if ~isempty(content{j}) & (~isfield(content{j}(1),'type') | ... Daniel@0: ~strcmp(content{j}(1).type,'som_norm')), Daniel@0: msg = 'Each cell in ''comp_norm'' should be either empty or type ''som_norm''.'; Daniel@0: isok = 0; Daniel@0: break; Daniel@0: end Daniel@0: end Daniel@0: if isok, sS.comp_norm = content; end Daniel@0: end Daniel@0: case 'trainhist', Daniel@0: if ~isstruct(content) & ~isempty(content), Daniel@0: msg = '''trainhist'' should be a struct array or empty.'; Daniel@0: else Daniel@0: isok = 1; Daniel@0: for j=1:length(content), Daniel@0: if ~isfield(content(j),'type') | ~strcmp(content(j).type,'som_train'), Daniel@0: msg = 'Each cell in ''trainhist'' should be of type ''som_train''.'; Daniel@0: isok = 0; Daniel@0: break; Daniel@0: end Daniel@0: end Daniel@0: if isok, sS.trainhist = content; end Daniel@0: end Daniel@0: otherwise, Daniel@0: msg = ['Invalid field for map struct: ' field]; Daniel@0: end Daniel@0: Daniel@0: case 'som_data', Daniel@0: [dlen dim] = size(sS.data); Daniel@0: switch field, Daniel@0: case 'data', Daniel@0: [dummy dim2] = size(content); Daniel@0: if prod(si)==0, Daniel@0: msg = '''data'' is empty'; Daniel@0: elseif ~isnumeric(content), Daniel@0: msg = '''data'' should be numeric matrix.'; Daniel@0: elseif dim ~= dim2 & ~isempty(sS.data), Daniel@0: msg = 'New ''data'' must have the same dimension as old one.'; Daniel@0: else Daniel@0: sS.data = content; isok = 1; Daniel@0: end Daniel@0: case 'labels', Daniel@0: if isempty(content), Daniel@0: sS.labels = cell(dlen,1); isok = 1; Daniel@0: elseif size(content,1) ~= dlen, Daniel@0: msg = 'Length of ''labels'' must be equal to the number of data vectors.'; Daniel@0: elseif ~iscell(content) & ~ischar(content), Daniel@0: msg = '''labels'' must be a string array or a cell array/matrix.'; Daniel@0: else Daniel@0: isok = 1; Daniel@0: if ischar(content), content = cellstr(content); Daniel@0: elseif ~iscellstr(content), Daniel@0: l = prod(size(content)); Daniel@0: for j=1:l, Daniel@0: if ~ischar(content{j}), Daniel@0: if ~isempty(content{j}), Daniel@0: msg = 'Invalid ''labels'' array.'; Daniel@0: isok = 0; j Daniel@0: break; Daniel@0: else Daniel@0: content{j} = ''; Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: if isok, sS.labels = content; end Daniel@0: end Daniel@0: case 'name', Daniel@0: if ~ischar(content), Daniel@0: msg = '''name'' should be a string.'; Daniel@0: else Daniel@0: sS.name = content; isok = 1; Daniel@0: end Daniel@0: case 'comp_names', Daniel@0: if ~iscell(content) & ~ischar(content), Daniel@0: msg = '''comp_names'' should be a cell string or a string array.'; Daniel@0: elseif length(content) ~= dim, Daniel@0: msg = 'Length of ''comp_names'' should be equal to dim.'; Daniel@0: else Daniel@0: if ischar(content), content = cellstr(content); end Daniel@0: if size(content,1)==1, content = content'; end Daniel@0: sS.comp_names = content; Daniel@0: isok = 1; Daniel@0: end Daniel@0: case 'comp_norm', Daniel@0: if ~iscell(content) & length(content)>0, Daniel@0: msg = '''comp_norm'' should be a cell array.'; Daniel@0: elseif length(content) ~= dim, Daniel@0: msg = 'Length of ''comp_norm'' should be equal to dim.'; Daniel@0: else Daniel@0: isok = 1; Daniel@0: for j=1:length(content), Daniel@0: if ~isempty(content{j}) & (~isfield(content{j}(1),'type') | ... Daniel@0: ~strcmp(content{j}(1).type,'som_norm')), Daniel@0: msg = 'Each cell in ''comp_norm'' should be either empty or type ''som_norm''.'; Daniel@0: isok = 0; Daniel@0: break; Daniel@0: end Daniel@0: end Daniel@0: if isok, sS.comp_norm = content; end Daniel@0: end Daniel@0: case 'label_names', Daniel@0: if ~iscell(content) & ~ischar(content) & ~isempty(content), Daniel@0: msg = ['''label_names'' should be a cell string, a string array or' ... Daniel@0: ' empty.']; Daniel@0: else Daniel@0: if ~isempty(content), Daniel@0: if ischar(content), content = cellstr(content); end Daniel@0: if size(content,1)==1, content = content'; end Daniel@0: end Daniel@0: sS.label_names = content; Daniel@0: isok = 1; Daniel@0: end Daniel@0: otherwise, Daniel@0: msg = ['Invalid field for data struct: ' field]; Daniel@0: end Daniel@0: Daniel@0: case 'som_topol', Daniel@0: switch field, Daniel@0: case 'msize', Daniel@0: if ~isnumeric(content) | ~isvector | ~ispositive | ~isinteger, Daniel@0: msg = '''msize'' should be a vector with positive integer elements.'; Daniel@0: else Daniel@0: sS.msize = content; isok=1; Daniel@0: end Daniel@0: case 'lattice', Daniel@0: if ~ischar(content), Daniel@0: msg = '''lattice'' should be a string'; Daniel@0: elseif ~strcmp(content,'rect') & ~strcmp(content,'hexa'), Daniel@0: msg = ['Unknown lattice type: ' content]; Daniel@0: sS.lattice = content; isok = 1; Daniel@0: else Daniel@0: sS.lattice = content; isok = 1; Daniel@0: end Daniel@0: case 'shape', Daniel@0: if ~ischar(content), Daniel@0: msg = '''shape'' should be a string'; Daniel@0: elseif ~strcmp(content,'sheet') & ~strcmp(content,'cyl') & ... Daniel@0: ~strcmp(content,'toroid'), Daniel@0: msg = ['Unknown shape type: ' content]; Daniel@0: sS.shape = content; isok = 1; Daniel@0: else Daniel@0: sS.shape = content; isok = 1; Daniel@0: end Daniel@0: otherwise, Daniel@0: msg = ['Invalid field for topology struct: ' field]; Daniel@0: end Daniel@0: Daniel@0: case 'som_train', Daniel@0: switch field, Daniel@0: case 'algorithm', Daniel@0: if ~ischar(content), Daniel@0: msg = '''algorithm'' should be a string.'; Daniel@0: else Daniel@0: sS.algorithm = content; isok = 1; Daniel@0: end Daniel@0: case 'data_name', Daniel@0: if ~ischar(content), Daniel@0: msg = '''data_name'' should be a string'; Daniel@0: else Daniel@0: sS.data_name = content; isok = 1; Daniel@0: end Daniel@0: case 'neigh', Daniel@0: if ~ischar(content), Daniel@0: msg = '''neigh'' should be a string'; Daniel@0: elseif ~isempty(content) & ~strcmp(content,'gaussian') & ~strcmp(content,'ep') & ... Daniel@0: ~strcmp(content,'cutgauss') & ~strcmp(content,'bubble'), Daniel@0: msg = ['Unknown neighborhood function: ' content]; Daniel@0: sS.neigh = content; isok = 1; Daniel@0: else Daniel@0: sS.neigh = content; isok = 1; Daniel@0: end Daniel@0: case 'mask', Daniel@0: if size(content,1) == 1, content = content'; end Daniel@0: dim = size(content,1); %[munits dim] = size(sS.data); Daniel@0: if ~isnumeric(content) | size(content) ~= [dim 1], Daniel@0: msg = '''mask'' should be a column vector (size dim x 1).'; Daniel@0: else Daniel@0: sS.mask = content; isok = 1; Daniel@0: end Daniel@0: case 'radius_ini', Daniel@0: if ~isnumeric(content) | ~isscalar, Daniel@0: msg = '''radius_ini'' should be a scalar.'; Daniel@0: else Daniel@0: sS.radius_ini = content; isok = 1; Daniel@0: end Daniel@0: case 'radius_fin', Daniel@0: if ~isnumeric(content) | ~isscalar, Daniel@0: msg = '''radius_fin'' should be a scalar.'; Daniel@0: else Daniel@0: sS.radius_fin = content; isok = 1; Daniel@0: end Daniel@0: case 'alpha_ini', Daniel@0: if ~isnumeric(content) | ~isscalar, Daniel@0: msg = '''alpha_ini'' should be a scalar.'; Daniel@0: else Daniel@0: sS.alpha_ini = content; isok = 1; Daniel@0: end Daniel@0: case 'alpha_type', Daniel@0: if ~ischar(content), Daniel@0: msg = '''alpha_type'' should be a string'; Daniel@0: elseif ~strcmp(content,'linear') & ~strcmp(content,'inv') & ... Daniel@0: ~strcmp(content,'power') & ~strcmp(content,'constant') & ~strcmp(content,''), Daniel@0: msg = ['Unknown alpha type: ' content]; Daniel@0: sS.alpha_type = content; isok = 1; Daniel@0: else Daniel@0: sS.alpha_type = content; isok = 1; Daniel@0: end Daniel@0: case 'trainlen', Daniel@0: if ~isnumeric(content) | ~isscalar, Daniel@0: msg = '''trainlen'' should be a scalar.'; Daniel@0: else Daniel@0: sS.trainlen = content; isok = 1; Daniel@0: end Daniel@0: case 'time', Daniel@0: if ~ischar(content), Daniel@0: msg = '''time'' should be a string'; Daniel@0: else Daniel@0: sS.time = content; isok = 1; Daniel@0: end Daniel@0: otherwise, Daniel@0: msg = ['Invalid field for train struct: ' field]; Daniel@0: end Daniel@0: Daniel@0: case 'som_norm', Daniel@0: switch field, Daniel@0: case 'method', Daniel@0: if ~ischar(field), Daniel@0: msg = '''method'' should be a string.'; Daniel@0: else Daniel@0: sS.method = content; isok = 1; Daniel@0: end Daniel@0: case 'params', Daniel@0: sS.params = content; isok = 1; Daniel@0: case 'status', Daniel@0: if ~ischar(content), Daniel@0: msg = '''status'' should be a string'; Daniel@0: elseif ~strcmp(content,'done') & ~strcmp(content,'undone') & ... Daniel@0: ~strcmp(content,'uninit'), Daniel@0: msg = ['Unknown status type: ' content]; Daniel@0: sS.status = content; isok = 1; Daniel@0: else Daniel@0: sS.status = content; isok = 1; Daniel@0: end Daniel@0: otherwise, Daniel@0: msg = ['Invalid field for normalization struct: ' field]; Daniel@0: end Daniel@0: Daniel@0: case 'som_grid', Daniel@0: if any(strcmp(field,{'lattice', 'shape', 'msize', 'coord',... Daniel@0: 'line', 'linecolor', 'linewidth', ... Daniel@0: 'marker', 'markersize', 'markercolor', 'surf', ... Daniel@0: 'label', 'labelcolor', 'labelsize'})), Daniel@0: warning('No checking done on field identifier or content.'); Daniel@0: sS = setfield(sS,field,content); Daniel@0: isok = 1; Daniel@0: else Daniel@0: msg = ['Invalid field for grid struct: ' field]; Daniel@0: end Daniel@0: Daniel@0: otherwise, Daniel@0: error('Unrecognized structure.'); Daniel@0: Daniel@0: end Daniel@0: Daniel@0: msgs{i} = msg; Daniel@0: ok(i) = isok; Daniel@0: Daniel@0: end Daniel@0: Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% return Daniel@0: Daniel@0: if nargout < 2, Daniel@0: for i=1:p, Daniel@0: if ~isempty(msgs{i}), Daniel@0: if ~ok(i), fprintf(1,'[Error! '); Daniel@0: else fprintf(1,'[Notice '); Daniel@0: end Daniel@0: fprintf(1,'in setting %s] ',varargin{2*i-1}); Daniel@0: fprintf(1,'%s\n',msgs{i}); Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: Daniel@0: