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