wolffd@0: function som_write_cod(sMap, filename) wolffd@0: wolffd@0: %SOM_WRITE_COD Writes a map struct to ascii file in SOM_PAK format. wolffd@0: % wolffd@0: % som_write_cod(sMap,filename) wolffd@0: % wolffd@0: % som_write_cod(sMap,'map1.cod'); wolffd@0: % wolffd@0: % Input and output arguments: wolffd@0: % sMap (struct) self-organizing map structure wolffd@0: % filename (string) name of input file wolffd@0: % wolffd@0: % Note that much of the information in the map struct is lost. wolffd@0: % Typically, when saving map structs into files use the 'save' command. wolffd@0: % wolffd@0: % For more help, try 'type som_write_cod' or check out online documentation. wolffd@0: % See also SOM_READ_COD, SOM_READ_DATA, SOM_WRITE_DATA. wolffd@0: wolffd@0: %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: % wolffd@0: % som_write_cod wolffd@0: % wolffd@0: % PURPOSE wolffd@0: % wolffd@0: % Writes a self-organizing map struct to a file in SOM_PAK format. wolffd@0: % wolffd@0: % SYNTAX wolffd@0: % wolffd@0: % som_write_cod(sMap,filename); wolffd@0: % wolffd@0: % DESCRIPTION wolffd@0: % wolffd@0: % This function is offered for compatibility with SOM_PAK, a SOM wolffd@0: % software package in C. It writes map struct to files in SOM_PAK format. wolffd@0: % wolffd@0: % See SOM_READ_COD for description of the SOM_PAK map file format. wolffd@0: % Because the SOM_PAK package does not support many of the features and wolffd@0: % options of the SOM Toolbox, some of the information is changed, or even wolffd@0: % lost. wolffd@0: % wolffd@0: % SOM_PAK does not support 3- or higher dimensional map grids. These cannot wolffd@0: % be exported using this function. wolffd@0: % SOM_PAK always supposes that the map has 'sheet' shape. wolffd@0: % SOM_PAK only supports 'bubble' and 'gaussian' neighborhood functions. wolffd@0: % Any other neighborhood function is changed to 'gaussian'. wolffd@0: % SOM_PAK doesn't support component names. However, the component names are wolffd@0: % written on a comment line which begins with '#n '. Any spaces (' ') wolffd@0: % in the component names are replaced with underscores ('_'). wolffd@0: % Information on map name, mask, training history and normalizations is lost. wolffd@0: % wolffd@0: % This function is only offered for compatibility with SOM_PAK. In general, wolffd@0: % when saving map structs in files, use 'save filename.mat sMap'. This is wolffd@0: % faster and retains all information of the map. wolffd@0: % wolffd@0: % REQUIRED INPUT ARGUMENTS wolffd@0: % wolffd@0: % sMap (struct) the SOM struct to be written wolffd@0: % filename (string) the name of the input file wolffd@0: % wolffd@0: % EXAMPLES wolffd@0: % wolffd@0: % som_write_cod(sMap,'map1.cod'); wolffd@0: % wolffd@0: % SEE ALSO wolffd@0: % wolffd@0: % som_read_cod Read a map from a file in SOM_PAK format. wolffd@0: % som_read_data Reads data from an ascii file. wolffd@0: % som_write_data Writes data struct into a file in SOM_PAK format. wolffd@0: wolffd@0: % Copyright (c) 1997-2000 by the SOM toolbox programming team. wolffd@0: % http://www.cis.hut.fi/projects/somtoolbox/ wolffd@0: wolffd@0: % Version 1.0beta ecco 221097 wolffd@0: % Version 2.0beta ecco 030899, juuso 151199 wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: %% check arguments and initialize wolffd@0: wolffd@0: error(nargchk(2, 2, nargin)) % check no. of input args is correct wolffd@0: wolffd@0: % sMap wolffd@0: msize = sMap.topol.msize; % map grid size wolffd@0: mdim = length(msize); % map grid dimension wolffd@0: [munits dim] = size(sMap.codebook); % input space dimension wolffd@0: wolffd@0: % map dimension check: wolffd@0: % map dimensions higher than 2 are not supported by SOM_PAK wolffd@0: if mdim > 2, wolffd@0: error('Cannot write maps with higher dimension than two'); wolffd@0: end wolffd@0: wolffd@0: % in SOM_PAK the xy-indexing is used, while in Matlab ij-indexing wolffd@0: % therefore, the codebook vectors have to be reorganized wolffd@0: order = reshape([1:munits],msize); wolffd@0: order = reshape(order',[munits 1]); wolffd@0: msize = fliplr(msize); wolffd@0: wolffd@0: % open output file wolffd@0: fid = fopen(filename, 'w'); wolffd@0: if fid < 0, wolffd@0: error(['Cannot open file ' filename]); wolffd@0: end wolffd@0: wolffd@0: % check version wolffd@0: v = version; wolffd@0: ver_53_or_newer = (str2num(v(1:3)) >= 5.3); wolffd@0: wolffd@0: wolffd@0: [lines numlabs] = size(sMap.labels); wolffd@0: has_labels = zeros(lines, 1); wolffd@0: if ver_53_or_newer wolffd@0: has_labels = sum((~(cellfun('isempty', sMap.labels))), 2); wolffd@0: else wolffd@0: for i = 1:lines wolffd@0: for j = 1:numlabs wolffd@0: if ~isempty(sMap.labels{i,j}) wolffd@0: has_labels(i) = 1; break; wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: %% write map into a file wolffd@0: wolffd@0: % write header wolffd@0: wolffd@0: fprintf(fid, '%d %s ', dim, sMap.topol.lattice); % dimension and lattice wolffd@0: fprintf(fid, '%d ', msize); % map size wolffd@0: % neighborhood type ('ep' and 'cutgauss' are not supported by SOM_PAK; wolffd@0: % they are converted to 'gaussian') wolffd@0: if strcmp(sMap.neigh,'bubble'), fprintf(fid, 'bubble\n'); wolffd@0: else wolffd@0: if ~strcmp(sMap.neigh,'gaussian'), wolffd@0: warning(['Neighborhood type ''' sMap.neigh ''' converted to ''gaussian''']); wolffd@0: end wolffd@0: fprintf(fid,'gaussian\n'); wolffd@0: end wolffd@0: wolffd@0: % write the component names as a SOM_PAK comment line wolffd@0: wolffd@0: fprintf(fid,'#n '); wolffd@0: for i=1:dim, fprintf(fid, '%s ', strrep(sMap.comp_names{i},' ','_')); end wolffd@0: fprintf(fid,'\n'); wolffd@0: wolffd@0: % write codebook wolffd@0: wolffd@0: form = [repmat('%g ',[1 dim]) '\n']; wolffd@0: if ~has_labels % no labels; fast wolffd@0: fprintf(fid, form, sMap.codebook(order,:)'); wolffd@0: else % has labels; slow wolffd@0: for i=1:munits, wolffd@0: fprintf(fid, '%g ', sMap.codebook(order(i),:)); wolffd@0: wolffd@0: if has_labels(order(i)) wolffd@0: temp = ''; wolffd@0: if ver_53_or_newer wolffd@0: nonempty = ~(cellfun('isempty', sMap.labels(i,:))); wolffd@0: else wolffd@0: for j = 1:numlabs, nonempty(j) = ~isempty(sMap.labels{i, j}); end wolffd@0: end wolffd@0: labs = char(sMap.labels{order(i), nonempty}); wolffd@0: labs(:,end + 1) = ' '; wolffd@0: temp = reshape(labs',[1 prod(size(labs))]); wolffd@0: temp(findstr(' ', temp))=''; wolffd@0: fprintf(fid, '%s\n', temp(1:end-1)); wolffd@0: else wolffd@0: fprintf(fid, '\n'); wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: % close file wolffd@0: wolffd@0: if fclose(fid) wolffd@0: error(['Cannot close file ' filename]); wolffd@0: else wolffd@0: fprintf(2, 'map write ok\n'); wolffd@0: end wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: