function [stdat2]=addcomp2
%  Add a compound to the data base
% function st=addcomp
% You can choose to load 301's Mass or Mass & Energy Bal. structure
%   or you can add to the structure st0.
% The compound in the initial structure's first position is used to
%   set the type and size of the data added to each field.
%The structure with data for the added compound is returned.

global stdat2

disp('Choose a database to edit')
choice1=menu('Pick a database to add to','301 Mass Balance',...
'301 Mass & Energy Balance','Your D. B. in argument.');
if choice1 == 1
   load struct3
elseif choice1 == 2
   load struct4
elseif choice1 == 3
snm=input('Enter the name of your structure to add to: ','s');
load (snm)
end

flds=fieldnames(stdat2);
[nrowf,ncolf]=size(flds);
[nrows,ncols]=size(stdat2);
newcol = ncols + 1;

for m=1:nrowf
	typ=getfield(stdat2(1,1),flds{m});
	if ischar(typ)
       stdat2 = setfield(stdat2,{newcol},flds{m}, ' ' );
   else
	   nv=length(typ);
	   stdat2 = setfield(stdat2,{newcol},flds{m}, ones(1,length(typ))*NaN );
   end
end

nquit=1;
while nquit
	   flds2=cell(nrowf+1,1);
	   flds2{1,1}='Finished';
	   for k=1:nrowf
		   flds2{k+1,1}=flds{k,1};
	   end
	   choice2=0;
	   while choice2~=1
		   choice2=menu('Choose',flds2);
		   if choice2 == 1
		          
		
                           break
		   else
			   typ=getfield(stdat2(1,1),flds{choice2-1});
			   if ischar(typ)
				   sv=input([flds{choice2-1},'= ? '],'s');
			       stdat2=setfield(stdat2,{newcol},flds{choice2-1},sv);
			   else
				   fprintf('Give a vector of length %3.0f\n',length(typ))
				   sv=input([flds{choice2-1},'= ? ']);
				   stdat2=setfield(stdat2,{newcol},flds{choice2-1},sv);
			   end			   
		   end

		end
              break
             end

disp('Do You wish to save this database,')
disp('or just globalize to workspace?')
pick1 = menu('Choose:', 'Save Database', 'Do not Save');
if pick1 == 1
if choice1 == 3
eval (['save ', snm, ' stdat2']);
disp(['The updated structure has been saved as: ',snm]);
else
file=input('Enter the name of your structure: ','s');
eval(['save ',file,' stdat2']);
end
elseif pick1 == 2
disp('Remember that you must declare stdat2 global prior to executing this option')
end

clear ans




