function [value,flag] = harray_get(h,varargin)

% harray_get - get properties for harray elements
% -----------------------------------------------
%
% [value,flag] = harray_get(h,'field', ... )
%
% Input:
% ------
%  h - harray structure array
%  field - property field name 
%
% Output:
% -------
%  value - property field values
%  flag - success flag

%--------------------------------
% Author: Harold Figueroa
%--------------------------------
% $Revision: 976 $
% $Date: 2005-04-25 19:27:22 -0400 (Mon, 25 Apr 2005) $
%--------------------------------

%--
% create persistent tables of valid 'get' fields
%--

% TODO: provide a way of getting these fieldnames

% TODO: find a better way of getting these field names

% NOTE: field name comparisons are case insensitive

persistent HARRAY_GET_FIELDS AXES_GET_FIELDS;

if (isempty(HARRAY_GET_FIELDS))
	
	% NOTE: the harray fieldnames have to be lower, to access as we do below
	
	HARRAY_GET_FIELDS = fieldnames(h(1));
	
	AXES_GET_FIELDS = lower(fieldnames(get(h(1).axes)));
	
end

%--
% set field values to get
%--

% NOTE: this 'lower' should catch non-string inputs

field = lower(varargin);

%--
% get field values
%--

flag = 1;

for k = 1:length(field)
	
	switch (field{k})
		
		%--
		% get harray field values
		%--
		
		case (HARRAY_GET_FIELDS)
			
			% NOTE: this part of the code is not dynamic and may need to change
			
			% NOTE: this is the only field that can't be packed in a matrix
			
			if (~strcmp(field{k},'children'))
				value.(field{k}) = cell2mat({h.(field{k})}');
			else 
				value.(field{k}) = {h.(field{k})}';
			end 
									
		%--
		% get axes position
		%--
		
		% NOTE: this is computed from the index
		
		case ('position')
			
			lev = cell2mat({h.level}'); index = cell2mat({h.index}');
			
			for k = 1:length(h)
				
				if (lev(k) == 0)
					value.position(k,:) = [0,0];
				else
					value.position(k,:) = index(k,(2 * lev(k) - 1):(2 * lev(k)));
				end
				
			end
			
		%--
		% get component axes properties
		%--
				
		case (AXES_GET_FIELDS)
		
			% NOTE: the matlab get is case insensitive
			
			try
				value.(field{k}) = get(ax,field{k});
			catch
				flag = 0;
				disp(' ');
				disp(['WARNING: Unable to get axes property field ''', field{k}, '''.']);
			end
			
		%--
		% unrecognized 'get' field
		%--
		
		otherwise
			
			flag = 0;
			disp(' ');
			disp(['WARNING: Unrecognized ''get'' field ''', field{k}, '''.']);
			
	end
	
end

%--
% extract single field as output
%--

if (length(field) == 1)
	value = value.(field{1});
end
