wolffd@0: function sMap=sompak_init(sData,ft,init_type,cout,ct,xdim,ydim,topol,neigh) wolffd@0: wolffd@0: %SOMPAK_INIT Call SOM_PAK initialization programs from Matlab. wolffd@0: % wolffd@0: % sMap=sompak_init(sData,ft,init_type,cout,ct,xdim,ydim,topol,neigh) wolffd@0: % wolffd@0: % ARGUMENTS ([]'s are optional and can be given as empty: [] or '') wolffd@0: % sData (struct) data struct wolffd@0: % (matrix) data matrix wolffd@0: % (string) filename wolffd@0: % [ft] (string) 'pak' or 'box'. Argument must be defined, if input wolffd@0: % file is used. wolffd@0: % init_type (string) string 'rand' or 'linear' wolffd@0: % [cout] (string) filename for output SOM, if argument is not defined wolffd@0: % (i.e. argument is '[]') temporary file '__abcdef' is wolffd@0: % used in operations and *it_is_removed* after wolffd@0: % operations!!! wolffd@0: % [ct] (string) 'pak' or 'box'. Argument must be defined, if output wolffd@0: % file is used. wolffd@0: % xdim (scalar) Number of units of the map in x-direction. wolffd@0: % ydim (scalar) Number of units of the map in y-direction. wolffd@0: % topol (string) string 'hexa' or 'rect' wolffd@0: % neigh (string) string 'bubble' or 'gaussian'. wolffd@0: % wolffd@0: % RETURNS wolffd@0: % sMap (struct) map struct wolffd@0: % wolffd@0: % Calls SOM_PAK initialization programs (randinit and lininit) from wolffd@0: % Matlab. Notice that to use this function, the SOM_PAK programs must wolffd@0: % be in your search path, or the variable 'SOM_PAKDIR' which is a wolffd@0: % string containing the program path, must be defined in the wolffd@0: % workspace. SOM_PAK programs can be found from: wolffd@0: % http://www.cis.hut.fi/research/som_lvq_pak.shtml wolffd@0: % wolffd@0: % See also SOMPAK_TRAIN, SOMPAK_SAMMON, SOMPAK_INIT_GUI, wolffd@0: % SOMPAK_GUI, SOM_LININIT, SOM_RANDINIT. wolffd@0: wolffd@0: % Contributed to SOM Toolbox vs2, February 2nd, 2000 by Juha Parhankangas wolffd@0: % Copyright (c) by Juha Parhankangas wolffd@0: % http://www.cis.hut.fi/projects/somtoolbox/ wolffd@0: wolffd@0: % Juha Parhankangas 050100 wolffd@0: wolffd@0: nargchk(9,9,nargin); wolffd@0: wolffd@0: NO_FILE = 0; wolffd@0: if isstruct(sData); wolffd@0: sData=sData.data; wolffd@0: elseif ~(isreal(sData) | isstr(sData)) wolffd@0: error('Argument ''sData'' must be a struct or a real matrix.'); wolffd@0: else wolffd@0: if isempty(ft) wolffd@0: if isstr(sData) wolffd@0: error('Argument ''file_type'' must be defined when input file is used.'); wolffd@0: end wolffd@0: elseif strcmp(ft,'pak'); wolffd@0: sData=som_read_data(sData); wolffd@0: elseif strcmp(ft,'box') wolffd@0: new_var=diff_varname; wolffd@0: varnames=evalin('base','who'); wolffd@0: loadname=eval(cat(2,'who(''-file'',''',sData,''')')); wolffd@0: if any(strcmp(loadname{1},evalin('base','who'))) wolffd@0: assignin('base',new_var,evalin('base',loadname{1})); wolffd@0: evalin('base',cat(2,'load(''',sData,''');')); wolffd@0: new_var2=diff_varname; wolffd@0: wolffd@0: assignin('base',new_var2,evalin('base',loadname{1})); wolffd@0: assignin('base',loadname{1},evalin('base',new_var)); wolffd@0: evalin('base',cat(2,'clear ',new_var)); wolffd@0: sData=evalin('base',new_var2); wolffd@0: evalin('base',cat(2,'clear ',new_var2)); wolffd@0: else wolffd@0: evalin('base',cat(2,'load(''',sData,''');')); wolffd@0: sData=evalin('base',loadname{1}); wolffd@0: evalin('base',cat(2,'clear ',loadname{1})); wolffd@0: end wolffd@0: else wolffd@0: error('Argument ''ft'' must be a string ''pak'' or ''box''.'); wolffd@0: end wolffd@0: end wolffd@0: if isstr(init_type) wolffd@0: if strcmp(init_type,'rand') wolffd@0: if any(strcmp('SOM_PAKDIR',evalin('base','who'))) wolffd@0: init_command=cat(2,evalin('base','SOM_PAKDIR'),'randinit'); wolffd@0: else wolffd@0: init_command='randinit'; wolffd@0: end wolffd@0: elseif strcmp(init_type,'linear') wolffd@0: if any(strcmp('SOM_PAKDIR',evalin('base','who'))) wolffd@0: init_command=cat(2,evalin('base','SOM_PAKDIR'),'lininit'); wolffd@0: else wolffd@0: init_command='lininit'; wolffd@0: end wolffd@0: else wolffd@0: error('Argument ''init_type'' must be string ''rand'' or ''linear''.'); wolffd@0: end wolffd@0: else wolffd@0: error('Argument ''init_type'' must be string ''rand'' or ''linear''.'); wolffd@0: end wolffd@0: wolffd@0: if (isstr(cout) & isempty(cout)) | (~isstr(cout) & isempty(cout)) wolffd@0: NO_FILE = 1; wolffd@0: cout = '__abcdef'; wolffd@0: elseif ~isstr(cout) & ~isempty(cout) wolffd@0: error('Argument ''cout'' must be a string or ''[]''.'); wolffd@0: end wolffd@0: wolffd@0: if ~is_positive_integer(xdim) wolffd@0: error('Argument ''xdim'' must be a positive integer.'); wolffd@0: end wolffd@0: wolffd@0: if ~is_positive_integer(ydim) wolffd@0: error('Argument ''ydim'' must be a positive integer.'); wolffd@0: end wolffd@0: wolffd@0: if isstr(topol) wolffd@0: if isempty(topol) | (~strcmp(topol,'hexa') & ~strcmp(topol,'rect')) wolffd@0: error ('Argument ''topol'' must be either a string ''hexa'' or ''rect''.'); wolffd@0: end wolffd@0: else wolffd@0: error ('Argument ''topol'' must be either a string ''hexa'' or ''rect''.'); wolffd@0: end wolffd@0: wolffd@0: if isstr(neigh) wolffd@0: if isempty(neigh) | (~strcmp(neigh,'bubble') & ~strcmp(neigh,'gaussian')) wolffd@0: error(sprintf(cat(2,'Argument ''neigh'' must be either a string ',... wolffd@0: '''bubble'' or ''gaussian''.'))); wolffd@0: end wolffd@0: else wolffd@0: error(sprintf(cat(2,'Argument ''neigh'' must be either a string ',... wolffd@0: '''bubble'' or ''gaussian''.'))); wolffd@0: end wolffd@0: wolffd@0: som_write_data(sData, cout); wolffd@0: str=cat(2,init_command,sprintf(' -din %s -cout %s ', cout ,cout),... wolffd@0: sprintf('-topol %s ',topol),... wolffd@0: sprintf('-neigh %s ',neigh),... wolffd@0: sprintf('-xdim %d -ydim %d',xdim,ydim)); wolffd@0: wolffd@0: if isunix wolffd@0: unix(str); wolffd@0: else wolffd@0: dos(str); wolffd@0: end wolffd@0: wolffd@0: sMap=som_read_cod(cout); wolffd@0: wolffd@0: if ~NO_FILE wolffd@0: if isunix wolffd@0: unix(cat(2,'/bin/rm ',cout)); wolffd@0: else wolffd@0: dos(cat(2,'del ',cout)); wolffd@0: end wolffd@0: if strcmp(ct,'pak') wolffd@0: som_write_cod(sMap,cout); wolffd@0: disp(cat(2,'Output written to the file ',cout,'.')); wolffd@0: elseif strcmp(ct,'box') wolffd@0: eval(cat(2,'save ',cout,' sMap')); wolffd@0: disp(cat(2,'Output written to the file ',sprintf('''%s.mat''.',cout))); wolffd@0: end wolffd@0: else wolffd@0: sMap.name=cat(2,'SOM ',date); wolffd@0: if isunix wolffd@0: unix('/bin/rm __abcdef'); wolffd@0: else wolffd@0: dos('del __abcdef'); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: wolffd@0: function bool = is_positive_integer(x) wolffd@0: wolffd@0: bool = ~isempty(x) & isreal(x) & all(size(x) == 1) & x > 0; wolffd@0: if ~isempty(bool) wolffd@0: if bool & x~=round(x) wolffd@0: bool = 0; wolffd@0: end wolffd@0: else wolffd@0: bool = 0; wolffd@0: end wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: wolffd@0: function str = diff_varname(); wolffd@0: wolffd@0: array=evalin('base','who'); wolffd@0: wolffd@0: if isempty(array) wolffd@0: str='a'; wolffd@0: return; wolffd@0: end wolffd@0: wolffd@0: for i=1:length(array) wolffd@0: lens(i)=length(array{i}); wolffd@0: end wolffd@0: wolffd@0: wolffd@0: ind=max(lens); wolffd@0: wolffd@0: str(1:ind+1)='a'; wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: