function var = somui_sel_var(figname)
%SOMUI_SEL_VAR() selects a variable from Matlab's workspace
%
% ARGUMENTS
%
%   figname  (string)	A name for the window, 'Load' or 'Save'
%
% RETURNS
%
%   var  (string)  A chosen variable name, -1 if error
%
% See also SOMUI_SEL_FILE, SOMUI_LOAD, SOMUI_SAVE

% Copyright (c) 1997 by the SOM toolbox programming team.
% http://www.cis.hut.fi/projects/somtoolbox/

% Version 1.0beta Jukka 071197 


if (~exist('figname'))
  figname='SOM -- Select';
end;

str = evalin('base','who');

% FUTURE: change this listdlg to a window of own
%   Point: With a double click!
%   [s, v] = local_dlg(figname, str);
[s, v] = listdlg('PromptString','Select a variable:',...
                 'SelectionMode','single',...
                 'Name',figname, ...
                 'ListString',str);     

% listdlg returns s and v:
%   if OK clicked then v==1 and s is an index to ListString.
%   if Cancel clicked then v==0 and s==[]

if (v == 1)			% OK
  if (~isempty(str))
    var = str{s};
  else
    var = -2;			% OK clicked even there's nothing to choose
  end;
else
  var = -1;			% CANCEL
end

% for files use standard uigetfile/uiputfile


%%%%%%%%%%%%
%%%%%%%%%%%    end of main
%%%%%%%%%%%%

%%%%%%%%%%%%%%
% under construction

function [s, v] = local_dlg(figname, str)

s=0;
v=0;

a = figure('Units','normalized', ...
	'Name',figname, ...
	'NumberTitle','off', ...
	'WindowStyle', 'modal', ...
	'Position',[0.2 0.3 0.3 0.2], ...
	'Tag','somui_sel_var');

b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'BackgroundColor',[0.701961 0.701961 0.701961], ...
	'Position',[0.01 0.01 0.71 0.98], ...
	'Style','frame', ...
	'Tag','fr_main');

b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'BackgroundColor',[0.701961 0.701961 0.701961], ...
	'FontWeight','bold', ...
	'HorizontalAlignment','left', ...
	'Position',[0.03 0.82 0.67 0.15], ...
	'String','Select a variable:', ...
	'Style','text', ...
	'Tag','st_sel_var_title');

b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'BackgroundColor',[1 1 1], ...
	'Style','Listbox', ...
	'String',str, ...
	'Position', [0.05 0.05 0.62 0.73], ...
	'Tag','lb_sel_var', ...
	'Value', []);

b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','',...
	'Position',[0.74 0.1 0.23 0.2], ...
	'String','Save', ...
	'Tag','pb_save');

b = uicontrol('Parent',a, ...
	'Units','normalized', ...
	'Callback','delete(gcf)',...
	'Position',[0.74 0.35 0.23 0.2], ...
	'String','Cancel', ...
	'Tag','pb_cancel');

