function som_comp_vis(varargin)

if ~nargin

a = figure('Color',[0.8 0.8 0.8], ...
	'PaperType','a4letter', ...
	'Position',[775 403 560 420], ...
	'Tag','Fig2');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'BackgroundColor',[0.752941 0.752941 0.752941], ...
	'ListboxTop',0, ...
	'Position',[0.0223214 0.505952 0.205357 0.386905], ...
	'Style','frame', ...
	'Tag','Frame1');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'BackgroundColor',[0.752941 0.752941 0.752941], ...
	'FontSize',8, ...
	'FontWeight','demi', ...
	'HorizontalAlignment','left', ...
	'Position',[0.0334821 0.830357 0.0904018 0.046875], ...
	'String','LOAD', ...
	'Style','text', ...
	'Tag','StaticText1');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis loadMap', ...
	'FontWeight','demi', ...
	'Position',[0.0334821 0.758929 0.178571 0.0595238], ...
	'String','MAP', ...
	'Tag','Pushbutton1');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis loadData', ...
	'FontWeight','demi', ...
	'Position',[0.0334821 0.684524 0.178571 0.0595238], ...
	'String','DATA', ...
	'Tag','Pushbutton2');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis loadProjection', ...
	'FontWeight','demi', ...
	'Position',[0.0334821 0.610119 0.178571 0.0595238], ...
	'String','PROJECTION', ...
	'Tag','Pushbutton3');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis loadCMap', ...
	'FontWeight','demi', ...
	'Position',[0.0334821 0.535714 0.178571 0.0595238], ...
	'String','COLORMAP', ...
	'Tag','Pushbutton4');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'BackgroundColor',[0.752941 0.752941 0.752941], ...
	'ListboxTop',0, ...
	'Position',[0.237723 0.505952 0.205357 0.386905], ...
	'Style','frame', ...
	'Tag','Frame2');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis setMap', ...
	'Min',1, ...
	'Position',[0.25 0.744048 0.178571 0.0744048], ...
	'String',' ', ...
        'FontWeight','demi',...
	'Style','popupmenu', ...
	'Tag','hMap', ...
	'Value',1);
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis setData', ...
	'Min',1, ...
	'Position',[0.25 0.535714 0.178571 0.0744048], ...
	'String',' ', ...
	'Style','popupmenu', ...
	'Tag','hDataSets', ...
	'FontWeight','demi',...
	'Value',1);
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'FontSize',8, ...
	'FontWeight','demi', ...
	'HorizontalAlignment','left', ...
	'Position',[0.25 0.824405 0.102121 0.0357143], ...
	'String','MAPS', ...
	'Style','text', ...
	'Tag','StaticText2');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'FontSize',8, ...
	'FontWeight','demi', ...
	'HorizontalAlignment','left', ...
	'Position',[0.25 0.610119 0.143973 0.0357143], ...
	'String','DATA SETS', ...
	'Style','text', ...
	'Tag','StaticText3');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'ListboxTop',0, ...
	'Position',[0.450893 0.505952 0.205357 0.386905], ...
	'Style','frame', ...
	'Tag','Frame3');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis setDataFlag', ...
	'FontSize',8, ...
	'FontWeight','demi', ...
	'Position',[0.462054 0.818452 0.178571 0.0595238], ...
	'String','SHOW DATA', ...
	'Style','radiobutton', ...
	'Tag','hShowData');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis setDataDrawMode', ...
	'FontSize',8, ...
	'FontWeight','demi', ...
	'Position',[0.477679 0.752976 0.140625 0.0446429], ...
	'String','HITS', ...
        'Value',1,...
	'Style','radiobutton', ...
	'Tag','hHits');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis setDataHitMode', ...
	'FontSize',8, ...
        'Value',1,...
	'FontWeight','demi', ...
	'Position',[0.495536 0.705357 0.154018 0.0446429], ...
	'String','HIT: marker', ...
	'Style','radiobutton', ...
	'Tag','hHitMarker');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis setDataHitMode', ...
	'FontSize',8, ...
	'FontWeight','demi', ...
	'Position',[0.495536 0.660714 0.154018 0.0446429], ...
	'String','HIT: number', ...
	'Style','radiobutton', ...
	'Tag','hHitNumber');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis setDataDrawMode', ...
	'FontSize',8, ...
	'FontWeight','demi', ...
	'Position',[0.477679 0.589286 0.140625 0.0446429], ...
	'String','XY-PLOT', ...
	'Style','radiobutton', ...
	'Tag','hXY');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'BackgroundColor',[0.752941 0.752941 0.752941], ...
	'ListboxTop',0, ...
	'Position',[0.0223214 0.235119 0.633929 0.252976], ...
	'Style','frame', ...
	'Tag','Frame4');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'FontSize',8, ...
	'FontWeight','demi', ...
	'HorizontalAlignment','left', ...
	'Position',[0.03125 0.440476 0.178571 0.0357143], ...
	'String','COORDINATES', ...
	'Style','text', ...
	'Tag','StaticText4');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'BackgroundColor',[0.752941 0.752941 0.752941], ...
	'FontSize',8, ...
	'FontWeight','demi', ...
	'HorizontalAlignment','left', ...
	'Position',[0.031808 0.407738 0.0892857 0.0357143], ...
	'String','MAP-X', ...
	'Style','text', ...
	'Tag','StaticText5');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis updateVisStruct', ...
	'Min',1, ...
	'Position',[0.0321429 0.330952 0.178571 0.0738095], ...
	'String',' ', ...
        'FontWeight','demi',...
	'Style','popupmenu', ...
	'Tag','hXCoord', ...
	'Value',1);
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis updateVisStruct', ...
	'Min',1, ...
	'Position',[0.25 0.330357 0.178571 0.0744048], ...
	'String',' ', ...
        'FontWeight','demi',...
	'Style','popupmenu', ...
	'Tag','hYCoord', ...
	'Value',1);
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis updateVisStruct', ...
	'Min',1, ...
	'Position',[0.462054 0.330357 0.178571 0.0744048], ...
	'String',' ', ...
	'Style','popupmenu', ...
        'FontWeight','demi',...
	'Tag','hZCoord', ...
	'Value',1);
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'FontSize',8, ...
	'FontWeight','demi', ...
	'HorizontalAlignment','left', ...
	'Position',[0.25 0.407738 0.0892857 0.0357143], ...
	'String','MAP-Y', ...
	'Style','text', ...
	'Tag','StaticText6');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'BackgroundColor',[0.701961 0.701961 0.701961], ...
	'FontSize',8, ...
	'FontWeight','demi', ...
	'HorizontalAlignment','left', ...
	'Position',[0.462054 0.407738 0.0892857 0.0357143], ...
	'String','MAP-Z', ...
	'Style','text', ...
	'Tag','StaticText7');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis updateVisStruct', ...
	'Min',1, ...
	'Position',[0.031808 0.238095 0.178571 0.0744048], ...
	'String',' ', ...
	'Style','popupmenu', ...
        'FontWeight','demi',...
	'Tag','hXData', ...
	'Value',1);
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis updateVisStruct', ...
	'Min',1, ...
	'Position',[0.25 0.238095 0.178571 0.0744048], ...
	'String',' ', ...
	'Style','popupmenu', ...
	'Tag','hYData', ...
        'FontWeight','demi',...
	'Value',1);
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis updateVisStruct', ...
	'Min',1, ...
	'Position',[0.462054 0.238095 0.178571 0.0744048], ...
	'String',' ', ...
	'Style','popupmenu', ...
        'FontWeight','demi',...
	'Tag','hZData', ...
	'Value',1);
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'BackgroundColor',[0.752941 0.752941 0.752941], ...
	'FontSize',8, ...
	'FontWeight','demi', ...
	'HorizontalAlignment','left', ...
	'Position',[0.031808 0.3125 0.0970982 0.0297619], ...
	'String','DATA-X', ...
	'Style','text', ...
	'Tag','StaticText8');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'FontSize',8, ...
	'FontWeight','demi', ...
	'HorizontalAlignment','left', ...
	'Position',[0.25 0.3125 0.113839 0.0297619], ...
	'String','DATA-Y', ...
	'Style','text', ...
	'Tag','StaticText9');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'FontSize',8, ...
	'FontWeight','demi', ...
	'HorizontalAlignment','left', ...
	'Position',[0.462054 0.3125 0.13058 0.0297619], ...
	'String','DATA-Z', ...
	'Style','text', ...
	'Tag','StaticText10');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'BackgroundColor',[0.752941 0.752941 0.752941], ...
	'ListboxTop',0, ...
	'Position',[0.0223214 0.0119048 0.633929 0.21131], ...
	'Style','frame', ...
	'Tag','Frame5');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis updateVisStruct', ...
	'Min',1, ...
	'Position',[0.031808 0.104167 0.178571 0.0744048], ...
	'String',getMarkerStr, ...
	'FontWeight','demi',...
	'Style','popupmenu', ...
	'Tag','hMarker', ...
	'Value',1);
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis updateVisStruct', ...
	'Min',1, ...
	'Position',[0.25 0.104167 0.178571 0.0744048], ...
	'String',' ', ...
	'Style','popupmenu', ...
	'Tag','hColor', ...
        'FontWeight','demi',...
	'Value',1);
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis updateVisStruct', ...
	'Min',1, ...
	'Position',[0.462054 0.104167 0.178571 0.0744048], ...
	'String',' ', ...
	'Style','popupmenu', ...
	'Tag','hSize', ...
        'FontWeight','demi',...
	'Value',1);
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'FontSize',8, ...
	'FontWeight','demi', ...
	'HorizontalAlignment','left', ...
	'Position',[0.031808 0.178571 0.129464 0.0357143], ...
	'String','MARKER', ...
	'Style','text', ...
	'Tag','StaticText11');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'FontSize',8, ...
	'FontWeight','demi', ...
	'HorizontalAlignment','left', ...
	'Position',[0.25 0.178571 0.148996 0.0357143], ...
	'String','COLOR', ...
	'Style','text', ...
	'Tag','StaticText12');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'FontSize',8, ...
	'FontWeight','demi', ...
	'HorizontalAlignment','left', ...
	'Position',[0.462054 0.178571 0.137277 0.0357143], ...
	'String','SIZE', ...
	'Style','text', ...
	'Tag','StaticText13');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'BackgroundColor',[0.752941 0.752941 0.752941], ...
	'Callback','som_comp_vis setSurf', ...
	'FontSize',8, ...
	'FontWeight','demi', ...
	'Position',[0.031808 0.0357143 0.178571 0.0595238], ...
	'String','Surf', ...
	'Style','radiobutton', ...
	'Tag','hSurf');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis setGridFlag', ...
	'FontSize',8, ...
	'FontWeight','demi', ...
	'Position',[0.25 0.0357143 0.178571 0.0595238], ...
	'String','Connections', ...
	'Style','radiobutton', ...
	'Tag','hGrid');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'BackgroundColor',[0.752941 0.752941 0.752941], ...
	'Callback','som_comp_vis setHold', ...
	'FontSize',8, ...
	'FontWeight','demi', ...
	'Position',[0.462054 0.0357143 0.178571 0.0595238], ...
	'String','Hold', ...
	'Style','radiobutton', ...
	'Tag','hHold');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','close gcf', ...
	'FontWeight','demi', ...
	'Position',[0.669643 0.0297619 0.15625 0.0595238], ...
	'String','CLOSE', ...
	'Tag','Pushbutton5');
b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','som_comp_vis visualize', ...
	'FontWeight','demi', ...
	'Position',[0.669643 0.104167 0.15625 0.0595238], ...
	'String','VISUALIZE', ...
	'Tag','Pushbutton6');

udata.maps=[];
udata.dataSets = [];
udata.projections = [];
udata.cMaps = [];
udata.surf = 0;
udata.grid = 0;
udata.hold = 0;
udata.data = 0;
udata.dataDrawMode = 1;
udata.dataHitMode = 1;

udata.currMap=0;
udata.currData = 0;
udata.currProj = 0;
udata.currCMap = 0;
udata.sV=vis_struct;
udata.ZCoord = [];
udata.dataX = [];
udata.dataY = [];
set(gcf,'UserData',udata);

updateAll;
return;

end

arg = varargin{1};

switch arg
  case 'loadData'
   loadFromWspace('data');
  case 'loadMap'
   loadFromWspace('map');
  case 'loadCMap'
   loadFromWspace('colormap');
  case 'loadProjection'
   loadFromWspace('projection');
  case 'updateVisStruct'
   str = get(gcbo,'Tag');
   updateVisStruct(str);
  case 'visualize'
   visualizeStruct;
  case 'setDataFlag'
   setDataFlag;
  case 'setDataDrawMode'
   setDataDrawMode;
  case 'setGridFlag'
   setGridFlag;
  case 'setDataHitMode'
   setDataHitMode;
  case 'setSurf'
   setSurf;
  case 'setMap'
   setMap;
  case 'setData'
   setData;
  case 'setHold'
   setHold;
end

return;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function updateAll(str)

updatePopup('x');
updatePopup('y');
updatePopup('z');
updatePopup('color');
updatePopup('size');
updatePopup('data');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function updatePopup(str)

switch str
  case {'x','y','z'}
   hStr = ['h' upper(str) 'Coord'];
   h=findobj(get(gcf,'Children'),'Tag',hStr);
   set(h,'String',getCoordStr(str));
  case 'color'
   h=findobj(get(gcf,'Children'),'Tag','hColor');
   set(h,'String',getColorStr);
  case 'size'
   h = findobj(get(gcf,'Children'),'Tag','hSize');
   set(h,'Value',1);
   set(h,'String',getSizeStr);
  case 'data'
   h = findobj(get(gcf,'Children'),'Tag','hXData');
   set(h,'Value',1);
   set(h,'String',getDataStr);
   h = findobj(get(gcf,'Children'),'Tag','hYData');
   set(h,'Value',1);
   set(h,'String',getDataStr);   
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function str = getCoordStr(coord)

udata=get(gcf,'UserData');
if ~udata.currMap
  str = {' '};
  return;
end

str = udata.maps(udata.currMap).comp_names;
for i=1:length(str)
  str{i} = ['<' str{i} '>'];
end
if coord == 'x'
  str = [str;{'<grid X>'}];
elseif coord == 'y'
  str = [str;{'<grid Y>'}]; 
end

for i=1:length(udata.projections)
  str = [str;{sprintf('<P%d - %s>',i,upper(coord))}];
end

str = [str;{'<set>';'<none>'}];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function str = getColorStr

udata=get(gcf,'UserData');

if ~udata.currMap
  str = {' '};
  return;
end

str = udata.maps(udata.currMap).comp_names;
for i=1:length(str)
  str{i} = ['<' str{i} '>'];
end
for i=1:length(udata.cMaps)
  str = [str;{sprintf('<Cmap %d>',i)}];
end

str = [str;{'<set>';'<none>'}];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function str = getSizeStr

udata = get(gcf,'UserData');
if ~udata.currMap
  str = {' '};
  return;
end

str = udata.maps(udata.currMap).comp_names;
for i=1:length(str)
  str{i} = ['<' str{i} '>'];
end

str = [str;{'<set>'}];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function str = getDataStr(coord)

udata=get(gcf,'UserData');
if ~udata.currData
  str = {' '};
  return;
end

str = udata.dataSets(udata.currData).comp_names;
for i=1:length(str)
  str{i} = ['<' str{i} '>'];
end
str = [str;{'<set>'}];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function loadFromWspace(handleType)

ch = get(gcf,'Children');
udata = get(gcf,'UserData');
str = 'Give the name of the ';

if ~any(strcmp(handleType,{'map','data','projection','colormap'}))
  errordlg(['Invalid object type ''' handleType '''.']);
  return;
end

str = [str handleType '.'];
str = inputdlg(str);

if isempty(str) | isempty(str{1})
  return;
end
str=str{1};
variables = evalin('base','who');
if any(strcmp(str,variables))
  item = evalin('base',str);
else
  errordlg(sprintf('Variable ''%s'' is not defined.',str));
  return;
end

switch handleType
  case {'map','data'}
   if strcmp(handleType,'data') & ~udata.currMap
     errordlg('Map is not loaded.');
     return;
   end
   if isstruct(item)
     if strcmp(handleType,'map')
       names = fieldnames(som_map_struct(1));
     else
       names = fieldnames(som_data_struct(1));
     end
     fieldsOfItem = fieldnames(item);
     for i=1:length(fieldsOfItem)
       if ~any(strcmp(fieldsOfItem{i},names))
         errordlg(['Invalid field ''' fieldsOfItem{i} '''.']);
         return;
       end
     end
   else
     errordlg('Variable is not a struct.');
     return;
   end                
 case 'projection'
  if ~udata.currMap
    errordlg('Map is not loaded.');
    return;
  end
  noOfNodes=prod(udata.maps(udata.currMap).topol.msize);
  if ~(vis_valuetype(item,{'nx3','nx2'}) & length(item) == noOfNodes)
    errordlg(sprintf('Projection should be a %dx3 matrix.',noOfNodes));
    return;
  end
  if vis_valuetype(item,{'nx2'})
    item = [item ones(noOfNodes,1)];
  end
 case 'colormap'
  noOfNodes=prod(udata.maps(udata.currMap).topol.msize);
  if ~vis_valuetype(item,{'1x3rgb'})
    if vis_valuetype(item,{'nx1'}) & length(item) ~= noOfNodes
       errordlg(sprintf('Colormap should be a %sx1 or a %sx3rgb matrix.',...
			noOfNodes,noOfNodes));
       return;
    elseif vis_valuetype(item,{'nx3rgb'}) & size(item,1) ~= noOfNodes
      errordlg(sprintf('Colormap should be a %dx1 or a %dx3rgb matrix.',...
			noOfNodes,noOfNodes));
      return;
    elseif ~vis_valuetype(item,{'nx1','nx3rgb'})
      errordlg(sprintf('Colormap should be a %dx1 or a %dx3rgb matrix.',...
			noOfNodes,noOfNodes));
      return;
    end
  end
end

value = item;

switch handleType
  case 'map'
   if ~udata.currMap;
     udata.maps = value;
     udata.currMap = 1;
     udata.ZCoord = [];
     set(gcf,'UserData',udata);
     initializeVisStruct;
     updateAll;
     initializePopup;
   else
     udata.maps=[udata.maps value];
     set(gcf,'UserData',udata);
   end   
     updateStruct('hMap');
  case 'data'
   if ~udata.currData
     udata.dataSets = value;
     udata.currData = 1;
     set(gcf,'UserData',udata);
     updatePopup('data');
   else
     udata.dataSets = [udata.dataSets value];
     set(gcf,'UserData',udata);
   end
   updateStruct('hDataSets');
  case 'projection'
   tmp.proj=value;
   if ~udata.currProj
     udata.projections = tmp;
     udata.currProj = 1;
     set(gcf,'UserData',udata);
   else
     udata.projections = [udata.projections tmp];
     set(gcf,'UserData',udata);
   end
   setNewPosition('hXCoord');
   setNewPosition('hYCoord');
   setNewPosition('hZCoord'); 
   updateStruct('hProjections');
  case 'colormap'
   tmp.cMap = value;
   if ~udata.currCMap
     udata.cMaps = tmp;
     udata.currCMap = 1;
     set(gcf,'UserData',udata);
   else
     udata.cMaps = [udata.cMaps tmp];
     set(gcf,'UserData',udata);
   end
   setNewPosition('hColor');
   updateStruct('hColorMaps');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function initializeVisStruct

udata=get(gcf,'UserData');
sV=vis_struct;
sV.linewidth = 0.5;
sV.labelsize=6;
sV.labelcolor='g';
sV.topol = udata.maps(udata.currMap).topol;
munits = prod(udata.maps(udata.currMap).topol.msize);
sV.labels = cell(munits,1);
sV.coord = NaN*ones(munits,2);
sV.color = 'none';
udata.sV=sV;

set(gcf,'UserData',udata);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function initializePopup

inds = getIndexStruct;
XYValue = inds.components + 1 + inds.projs +2;
ZValue = inds.components + inds.projs + 2;
ColorValue = inds.components + inds.cMaps +2;
SizeValue = inds.components + 1;

h = findobj(get(gcf,'Children'),'Tag','hXCoord');
set(h,'Value',XYValue);
h = findobj(get(gcf,'Children'),'Tag','hYCoord');
set(h,'Value',XYValue);
h = findobj(get(gcf,'Children'),'Tag','hZCoord');
set(h,'Value',ZValue);
h = findobj(get(gcf,'Children'),'Tag','hColor');
set(h,'Value',ColorValue);
h = findobj(get(gcf,'Children'),'Tag','hSize');
set(h,'Value',SizeValue);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function ind = getIndexStruct;

ind.components = 0;
ind.cMaps = 0;
ind.projs = 0;
ind.dataSets=0;
ind.dataComps = 0;
udata=get(gcf,'UserData');
if length(udata.maps)
  ind.components = length(udata.maps(udata.currMap).comp_names);
end
ind.cMaps=length(udata.cMaps);
ind.projs=length(udata.projections);
ind.dataSets=length(udata.dataSets);
if ind.dataSets
  ind.dataComps = length(udata.dataSets(udata.currData).comp_names);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function updateStruct(arg)

udata=get(gcf,'UserData');
h = findobj(get(gcf,'Children'),'Tag',arg);
str = get(h,'String');
switch(arg)
  case 'hMap'
   newName=udata.maps(end).name;
   newName=sprintf('#%d: %s',length(udata.maps),newName);  
  case 'hDataSets'
   newName=udata.dataSets(end).name;
   newName=sprintf('#%d: %s',length(udata.dataSets),newName);
  case 'hProjections'
   newName=sprintf('#%d: <Proj%d>',length(udata.projections),...
                                   length(udata.projections)); 
  case 'hColorMaps'
   newName=sprintf('#%d: <CMap%d>< ',length(udata.cMaps),length(udata.cMaps)); 
end

if ~iscell(str)
  str = {newName};
else
  str = [str;{newName}];
end
set(h,'String',str);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function setNewPosition(arg)

h=findobj(get(gcf,'Children'),'Tag',arg);
oldstr = getfield(get(h,'String'),{get(h,'Value')});


switch(arg)
  case 'hColor'
   updatePopup('color');
  case 'hXCoord'
   updatePopup('x');
  case 'hYCoord'
   updatePopup('y');
  case 'hZCoord'
   updatePopup('z');
end

if isempty(oldstr) | isempty(oldstr{1})
  return;
end


str = get(h,'String');
newpos = find(strcmp(oldstr,str));
set(h,'Value',newpos);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function updateVisStruct(arg)



udata=get(gcf,'UserData');
if ~udata.currMap
  return;
end
h = findobj(get(gcf,'Children'),'Tag',arg);
value = get(gcbo,'Value');
indices = getIndexStruct;

switch arg
  case {'hXCoord','hYCoord'}
   if value > indices.components
     if value == indices.components +1
       if arg(2) == 'X'
         setGrid('X');
         return;
       else
         setGrid('Y');
         return;
       end
     end
     if value > indices.components + indices.projs +1
       if value > indices.components+indices.projs +2
         if arg(2) == 'X'
           udata.sV.coord(:,1) = ...
           NaN*ones(size(udata.maps(udata.currMap).codebook,1),1);
         else
           udata.sV.coord(:,2) = ...
           NaN*ones(size(udata.maps(udata.currMap).codebook,1),1);
         end 
       else
         if arg(2) == 'X'
           setValue('X');
           return;
         else
           setValue('Y');
           return;
         end
       end
     else
       if arg(2) == 'X'
         udata.sV.coord(:,1) = udata.projections(udata.currProj).proj(:,1);
       else
         udata.sV.coord(:,2) = udata.projections(udata.currProj).proj(:,2);
       end
     end
   else
     if isstr(udata.sV.coord);
      udata.sV.coord = NaN*ones(prod(udata.maps(udata.currMap).topol.msize),2);
     end
     if arg(2) == 'X'
       udata.sV.coord(:,1) = udata.maps(udata.currMap).codebook(:,value);
     else
       udata.sV.coord(:,2) = udata.maps(udata.currMap).codebook(:,value);
     end
   end
  case 'hZCoord'
   if value > indices.components
     if value > indices.components+indices.projs
       if value == indices.components+indices.projs + 1
         setValue('Z');
         return;
       else
         udata.ZCoord = [];
       end
     else
       udata.ZCoord = udata.projections(udata.currProj).proj(:,3);
     end
   else
     udata.ZCoord = udata.maps(udata.currMap).codebook(:,value);
   end

  case 'hMarker'
   udata.sV.node = getfield(get(h,'String'),{get(h,'Value')});
   udata.sV.node=udata.sV.node{1};
   set(gcf,'UserData',udata);
   if any(strcmp(udata.sV.node,{'hexa','rect'}))
     hx = findobj(get(gcf,'Children'),'Tag','hXCoord');
     hy = findobj(get(gcf,'Children'),'Tag','hYCoord');
     if strcmp(getfield(get(hx,'String'),{get(hx,'Value')}),'<grid X>')
       setGrid('X');
     end
     if  strcmp(getfield(get(hy,'String'),{get(hy,'Value')}),'<grid Y>')
       setGrid('Y');
     end
     return;
   end
  case 'hColor'
   if value > indices.components
     if value > indices.components + indices.cMaps
       if value == indices.components + indices.cMaps + 1
        setValue('Color');
        return;
       else
        udata.sV.color = 'none';
       end
     else
       if size(udata.cMaps(value-indices.components).cMap,1) ~= ...
          prod(udata.maps(udata.currMap).topol.msize)
          errordlg ('The current colormap has wrong dimensions.');
	  return;
       elseif size(udata.cMaps(value-indices.components).cMap,2) == 3
         udata.sV.color = udata.cMaps(value-indices.components).cMap;
       else
         udata.sV.color = 'indexed';
         udata.sV.data = udata.cMaps(value-indices.components).cMap;
       end 
     end
   else
     udata.sV.color = 'indexed';
     udata.sV.data = udata.maps(udata.currMap).codebook(:,value);    
   end
  case 'hSize'
   if value > indices.components
     setValue('Size');
     return;
   else
     udata.sV.size = udata.maps(udata.currMap).codebook(:,value);
   end
  case {'hXData','hYData'}
   if indices.dataComps
     if value > indices.dataComps
       setValue(arg(2:length(arg)));
       return;
     else
       if strcmp(arg,'hXData')
         udata.dataX = [];
       else
         udata.dataY = [];
       end
     end
   end
end

set(gcf,'UserData',udata);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function setGrid(arg)

udata = get(gcf,'UserData');
if ~udata.currMap
  return;
end
munits = prod(udata.maps(udata.currMap).topol.msize);
xdim = udata.maps(udata.currMap).topol.msize(2);
ydim = udata.maps(udata.currMap).topol.msize(1);

if strcmp(udata.sV.node,'rect')
  lattice = 'rect';
else
  lattice = 'hexa';
end

if isstr(udata.sV.coord)
  udata.sV.coord = NaN*ones(munits,2);
end

if arg == 'X'
 coord=repmat([1:xdim]',[ydim 1]);
 coord = sort(coord);
else
 coord=repmat([1:ydim]',[xdim 1]);
end

if strcmp(lattice,'hexa') & arg == 'X'
  inds = 2:2:ydim;
  k=length(inds);
  inds = repmat(inds',[xdim 1]);
  for i=1:xdim
    tmp=(1+(i-1)*k):(i*k);
    inds(tmp) = inds(tmp)+(i-1)*ydim;
  end
  coord(inds)=coord(inds)+0.5;

end
if arg == 'X'
  udata.sV.coord(:,1) = coord;
else
  udata.sV.coord(:,2) = coord;
end

set(gcf,'UserData',udata);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function setValue(arg)

udata = get(gcf,'UserData');
munits = prod(udata.maps(udata.currMap).topol.msize);

switch(arg)
  case {'X','Y','Z'}
    str = ['Give the ' arg '-coordinate vector:'];
    answer = inputdlg(str);
    if isempty(answer) | isempty(answer{1})
      return;
    end
    coord = evalin('base',answer{1});
    if ~isnumeric(coord) | ~all(size(coord) == [munits 1]) 
      errordlg([arg '-coordinate must be a ' num2str(munits) 'x1 -vector.']);
      return;
    end
    switch arg
      case 'X'
       value = 1;
      case 'Y'
       value = 2;
      otherwise
       value = 3;
    end
     if isstr(udata.sV.coord)
       udata.sV.coord = NaN * ones(munits,1);
     end
     if value == 1 | value == 2
       udata.sV.coord(:,value) = coord;
     else
       udata.ZCoord = coord;
     end
  case 'Color'
   str = 'Give the color matrix:';
   answer = inputdlg(str);
   if isempty(answer) | isempty(answer{1})
     return;
   end 
   color = evalin('base',answer{1});
   if ~isnumeric(color) | ...
      ~(vis_valuetype(color,{'nx1','nx3rgb'}) & size(color,1) == munits)
     errordlg(sprintf('Color must be %dx1 or %dx3rgb matrix.',munits,munits));
     return;
   end
   if size(color,2) == 3
     udata.sV.color = color;
   else
     udata.sV.color = 'indexed';
     udata.sV.data = color;
   end
 case 'Size'
   str = 'Give the size matrix:';
   answer = inputdlg(str);
   if isempty(answer) | isempty(answer{1})
     return;
   end 
   Size = evalin('base',answer{1});
   if ~isnumeric(Size) | ...
      ~(vis_valuetype(Size,{'nx1','1x1'}))
     errordlg(sprintf('Size must be %dx1 matrix or a scalar.',munits));
     return;
   end 
   if vis_valuetype(Size,{'nx1'}) & ~vis_valuetype(Size,{'1x1'}) & ...
     size(Size,1) ~= munits
     errordlg(sprintf('Size must be %dx1 matrix or a scalar.',munits));
     return; 
   end
   udata.sV.size = Size;
    
 case {'XData','YData'}
   str = 'Give the data coordinate matrix:';
   answer = inputdlg(str);
   if isempty(answer) | isempty(answer{1})
     return;
   end
   coord = eval(answer{1});
   if ~isnumeric(coord) | ~(size(coord,2) == 1)
     errordlg('Data coordinate has to be a nx1 vector.');
     return;
   end
   if arg(1) == 'X'
     udata.dataX = coord;
   else
     udata.dataY = coord;
   end
end

set(gcf,'UserData',udata);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function str = getMarkerStr

str={'hexa';'rect';'o';'+';'x';'*';'v';'^';'<';'>';'h';'s';'d';'p';'.'};

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function visualizeStruct
  udata=get(gcf,'UserData');
  h = findobj(get(0,'Children'),'Tag','figure_visfig2');
  if isempty(h)
    old = gcf;
    h = figure('Tag','figure_visfig2');
    set(0,'CurrentFigure',h);
  else
    old = gcf;
    set(0,'CurrentFigure',h);
  end

  if udata.hold
    hold on;
  else
    hold off;
    clf;
  end

%if ~udata.data | (udata.dataDrawMode == 1)
  if any(strcmp(udata.sV.node,{'hexa','rect'}))
    if isstr(udata.sV.color) & strcmp(udata.sV.color,'indexed')
      h2 = som_cplane(udata.sV.node,udata.sV.coord,udata.sV.data,...
                      udata.sV.size);
    else
      h2 = som_cplane(udata.sV.node,udata.sV.coord,udata.sV.color,...
                      udata.sV.size);
    end
    if ~isempty(udata.ZCoord)
      zcoord = udata.ZCoord';
      if strcmp(udata.sV.node,'hexa')
        zcoord = ones(6,1)*zcoord;
      else
        zcoord = ones(4,1)*zcoord;
      end
      set(h2,'ZData',zcoord);
      if udata.surf
        coord = [udata.sV.coord udata.ZCoord]; % for the surf...
      end
    elseif udata.surf | udata.grid
      coord = udata.sV.coord;
    end
    if udata.grid
      line = '-';
    else
      line = 'none';
    end
    surf = [];
    HOLD = ishold;
    hold on;
    if udata.surf
      if isstr(udata.sV.color) & strcmp(udata.sV.color,'indexed')
        surf = udata.sV.data;
      end
      if ~isstr(udata.sV.color)
        surf = udata.sV.color;
      end
    end
    if udata.surf | udata.grid
        som_grid(udata.sV.node,udata.sV.topol.msize,'Coord',coord,...
                 'Surf',surf,'Line',line,'Marker','none','Linecolor','k');
    end
    if ~HOLD
      hold off;
    end
  else
    surf = [];
    if udata.grid
      line = '-';
    else
      line = 'none';
    end
    if ~isempty(udata.ZCoord)
      udata.sV.coord =[udata.sV.coord udata.ZCoord];
    end
    if isstr(udata.sV.color) & any(strcmp(udata.sV.color,{'none','indexed'}))
       if strcmp(udata.sV.color,'indexed') & udata.surf
         surf = udata.sV.data;
       end
       udata.sV.color = 'k';
    end
    som_grid(udata.sV.topol.lattice,udata.sV.topol.msize,...
            'Coord',udata.sV.coord,...
            'Marker',udata.sV.node,'Surf',surf,'MarkerColor',udata.sV.color,...
            'LineColor',udata.sV.linecolor,'Linewidth',udata.sV.linewidth,...
            'Line',line,'MarkerSize',udata.sV.size);
  end
 
%end

if udata.data & udata.currData
 switch udata.dataDrawMode
  case 1
   if udata.currMap
     dataStruct = udata.dataSets(udata.currData);
     %if isstr(udata.sV.node) & strcmp(udata.sV.node,'hexa')
     %  coord=som_vis_coords('hexa',udata.sV.topol.msize);
     %else
     %  coord=som_vis_coords('rect',udata.sV.topol.msize);
     %end
     inds = unique(som_bmus(udata.maps(udata.currMap),dataStruct));
     coord = udata.sV.coord;      
     coord = coord(inds,:);
     hits = som_hits(udata.maps(udata.currMap),dataStruct);
     maxHit = max(hits);
     hits(find(hits == 0)) = [];
     hits = hits ./ maxHit; 
     HOLD = ishold;
     hold on;
     if udata.dataHitMode == 1
       %plot(coord(:,1),coord(:,2),'LineStyle','none','Marker','o',...
       %   'MarkerFaceColor','k','MarkerSize',udata.sV.size .* hits);
       [x,y]=pol2cart(0:0.1:2.1*pi,0.4);
        x=x';
        y=y';
        coordx = [];
        coordy = [];
        for i=1:length(hits);
          coordx = [coordx (x*hits(i) + coord(i,1))];
          coordy = [coordy (y*hits(i) + coord(i,2))];
        end
        patch(coordx,coordy,'k');
     else
       hits = som_hits(udata.maps(udata.currMap),dataStruct);
       hits = cellstr(num2str(hits));
       hits = hits(inds);
       text(coord(:,1),coord(:,2),hits,'HorizontalAlignment','left');
     end
     if ~ishold
       hold off;
     end
  end  
  case 2
   xVal = get(findobj(get(old,'Children'),'Tag','hXData'),'Value');
   yVal = get(findobj(get(old,'Children'),'Tag','hYData'),'Value');
   dataX = udata.dataX;
   dataY = udata.dataY;
   if isempty(dataX)
     dataX = udata.dataSets(udata.currData).data(:,xVal);
   end
   if isempty(dataY)
     dataY = udata.dataSets(udata.currData).data(:,yVal);
   end
   HOLD = ishold;
   hold on;
   plot(dataX,dataY,'LineStyle','none','Marker','o','MarkerFaceColor','k');
   if ~ishold
     hold off;
   end
 end
end
set(0,'CurrentFigure',old);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function setDataFlag

udata = get(gcf,'UserData');
udata.data = ~udata.data;
set(gcf,'UserData',udata);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function setGridFlag

udata=get(gcf,'UserData');
udata.grid = ~udata.grid;
set(gcf,'UserData',udata);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function setDataDrawMode

tag = get(gcbo,'Tag');
udata = get(gcf,'UserData');

switch tag
  case 'hHits'
   h = findobj(get(gcf,'Children'),'Tag','hXY');
   udata.dataDrawMode = 1;
  case 'hXY'
   h = findobj(get(gcf,'Children'),'Tag','hHits');
   udata.dataDrawMode = 2;
end

set(gcf,'UserData',udata);
set(h,'Value',0);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function setDataHitMode

udata = get(gcf,'UserData');
tag = get(gcbo,'Tag');

switch tag
  case 'hHitMarker'
   h = findobj(get(gcf,'Children'),'Tag','hHitNumber');
   udata.dataHitMode = 1;
  case 'hHitNumber'
   h = findobj(get(gcf,'Children'),'Tag','hHitMarker'); 
   udata.dataHitMode = 2;
end

set(gcf,'UserData',udata); 
set(h,'Value',0);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function setSurf

udata = get(gcf,'UserData');

udata.surf = ~udata.surf;
set(gcf,'UserData',udata);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function setMap

udata=get(gcf,'UserData');
if ~udata.currMap
  return;
end
value = get(gcbo,'Value');
if value == udata.currMap
  return;
end
udata.currMap=value;
udata.ZCoord = [];
set(gcf,'UserData',udata);
ch = get(gcf,'Children');
h = [findobj(ch,'Tag','hXCoord'), findobj(ch,'Tag','hYCoord'),...
     findobj(ch,'Tag','hZCoord'), findobj(ch,'Tag','hColor'),...
     findobj(ch,'Tag','hMarker')];
set(h,'Value',1);
initializeVisStruct;
updateAll;
initializePopup;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function setData;

udata=get(gcf,'UserData');
if ~udata.currData
  return;
end
value = get(gcbo,'Value');
if value == udata.currData
  return;
end

udata.currData = value;
udata.dataX = [];
udata.dataY = [];
set(gcf,'UserData',udata);

updatePopup('data');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function setHold

udata=get(gcf,'UserData');
udata.hold = ~udata.hold;
set(gcf,'UserData',udata);


