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