function value = set_env(name, value, flag)

% set_env - set environment variable
% ----------------------------------
%
% value = set_env(name, value, flag)
%
% Input:
% ------
%  name - variable name
%  value - variable value
%  flag - display flag (def: 0)
%
% Output:
% -------
%  value - value set

%--------------------------------
% Author: Harold Figueroa
%--------------------------------
% $Revision: 6105 $
% $Date: 2006-08-11 16:16:28 -0400 (Fri, 11 Aug 2006) $
%--------------------------------

% TODO: update the environment variable framework to use application data

% TODO: update framework to alloe for saving to a file

%--
% set display flag
%--

if (nargin < 3) || isempty(flag)
	flag = 0;
end

%--
% get root userdata
%--

data = get(0, 'userdata');

%--
% check for existing environment variable structure
%--

if isfield(data, 'env')
	
	if isfield(data.env, name)
		
		%--
		% update value of existing environment variable
		%--
		
		data.env.(name) = value;
		
	else
		
		%--
		% create new environment variable
		%--
		
		data.env.(name) = value;
		
		%--
		% report creation of new environment variable
		%--
		
		if flag
			disp(['Environment variable ''', name, ''' created.']);
		end
		
	end
	
else
	
	%--
	% create new environment variable
	%--
		
	data.env.(name) = value;
	
	%--
	% report creation of environment variable structure and new environment variable
	%--
	
	if flag
		disp(['Environment variable structure created.']);
		disp(['Environment variable ''', name, ''' created.']);
	end
	
end

%--
% update root userdata
%--

set(0, 'userdata', data);
