function varargout=gui_var(varargin)
global zeng2
if nargin==0
   action = 'initial';
else
   action = varargin{1};
end

switch action
case 'initial'
    if length(zeng2.var)==0;
        zexst('err','You have no variable to set.')
        return
    end
    GUIvar.parent=varargin{2};
    temp=findobj('type','figure','tag','gui_var');
    if ~isempty(temp);
        PointerLocation=get(0,'PointerLocation');
        FG_Size=[PointerLocation(1) 0 220 415];
        FG_Size(2)=PointerLocation(2)-FG_Size(4)-10;
        figure(temp)
        return
    end
    varnumber=1;
    temp=find(zeng2.var{varnumber}.name=='{');
    if isempty(temp)
        eval(['global ' zeng2.var{varnumber}.name])
    else
        eval(['global ' zeng2.var{varnumber}.name(1:(temp-1))])
    end
    %Needed for compiling reason
    Temppara=[];
    %---------------------------
    eval(['Temppara=' zeng2.var{varnumber}.name ';'])
    parent_size=get(GUIvar.parent,'position');
    if parent_size(2)<400
        temp=    parent_size(2)+30+116+70;
    else
        temp=    parent_size(2)-30-116-415-20;
    end;
    FG_Size=[parent_size(1) temp  220 415];
    GUIvar.figure = figure('Color',[0.8 0.8 0.8], ...
        'MenuBar','none',...
        'Name','Setting:Parameters',...
        'NumberTitle','off',...
	    'Position',FG_Size, ...
        'tag','gui_var',...
    	'ToolBar','none');
    set(GUIvar.figure,'position',FG_Size)
    FG_Backgournd=get(GUIvar.figure,'Color');
    uicontrol('Parent',GUIvar.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[0.85 0.8 0.75], ...
        'callback','gui_var(''okay'')',...
	    'ListboxTop',0, ...
	    'Position',[25 5 50 22], ...
        'String','Ok');
    uicontrol('Parent',GUIvar.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[0.85 0.8 0.75], ...
        'callback','delete(gcbf)',...
	    'ListboxTop',0, ...
	    'Position',[85 5 50 22], ...
	    'String','Close');
    uicontrol('Parent',GUIvar.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[0.85 0.8 0.75], ...
        'callback','gui_var(''okay'',''apply'')',...
	    'ListboxTop',0, ...
	    'Position',[145 5 50 22], ...
	    'String','Apply');
    var_string=cell(1,length(zeng2.var));
    for i=1:length(zeng2.var)
        var_string{i}=zeng2.var{i}.name;
    end
    uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',FG_Backgournd, ...
	    'HorizontalAlignment','right', ...
	    'ListboxTop',0, ...
	    'Position',[10 385 100 22], ...
	    'String','Variable :', ...
	    'Style','text');        
    GUIvar.var = uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',[1 1 1], ...
        'callback','gui_var(''update number'');',...
	    'ListboxTop',0, ...
	    'Position',[120 385 85 22], ...
        'string',var_string,...
    	'Style','popupmenu');
    uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',FG_Backgournd, ...
	    'HorizontalAlignment','right', ...
	    'ListboxTop',0, ...
	    'Position',[10 360 100 22], ...
	    'String','nseg :', ...
	    'Style','text');        
    GUIvar.nseg = uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[120 360 85 22], ...
        'string',num2str(Temppara.nseg),...
	    'Style','edit');
    uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',FG_Backgournd, ...
	    'HorizontalAlignment','right', ...
	    'ListboxTop',0, ...
	    'Position',[10 335 100 22], ...
	    'String','dia (um)', ...
	    'Style','text');        
    GUIvar.dia = uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[120 335 85 22], ...
        'string',num2str(Temppara.dia),...
	    'Style','edit');
    uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',FG_Backgournd, ...
	    'HorizontalAlignment','right', ...
	    'ListboxTop',0, ...
	    'Position',[10 310 100 22], ...
	    'String','Daxon/Dia ', ...
	    'Style','text');        
    GUIvar.da_D = uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[120 310 85 22], ...
        'string',num2str(Temppara.da_D),...
	    'Style','edit');
    uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',FG_Backgournd, ...
	    'HorizontalAlignment','right', ...
	    'ListboxTop',0, ...
	    'Position',[10 285 100 22], ...
	    'Style','text', ...
	    'String','Dnode/Dia');        
    GUIvar.dn_D = uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[120 285 85 22], ...
        'string',num2str(Temppara.dn_D),...
	    'Style','edit');
    GUIvar.Lmode=uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',[1 1 1], ...
        'callback','gui_var(''update Lmode'');',...
	    'HorizontalAlignment','right', ...
	    'ListboxTop',0, ...
	    'Position',[10 260 100 22], ...
        'string',[{'L=fix(um)','L=n.dia.(Linter/D)'}],...
    	'Style','popupmenu', ...
        'value',Temppara.Lmode);        
    if Temppara.Lmode==1 %1=fix , 2=(Linter/D).n.D
        Lenable='on';
        Lienable='off';
    else
        Lenable='off';
        Lienable='on';
    end
    GUIvar.L = uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[120 260 85 22], ...
	    'Style','edit', ...
        'string',num2str(Temppara.L),...
        'enable',Lenable);
    uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',FG_Backgournd, ...
	    'HorizontalAlignment','right', ...
	    'ListboxTop',0, ...
	    'Position',[10 235 100 22], ...
	    'String','Linter/Dia', ...
	    'Style','text');        
    GUIvar.Linter_D = uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[120 235 85 22], ...
	    'Style','edit', ...
        'string',num2str(Temppara.Linter_D),...
        'enable',Lienable);
    uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',FG_Backgournd, ...
	    'HorizontalAlignment','right', ...
	    'ListboxTop',0, ...
	    'Position',[10 210 100 22], ...
	    'Style','text', ...
	    'String','Lnodal (um)');        
    GUIvar.lnodal = uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[120 210 85 22], ...
        'string',num2str(Temppara.lnodal),...
	    'Style','edit');
    uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',FG_Backgournd, ...
	    'HorizontalAlignment','right', ...
	    'ListboxTop',0, ...
	    'Position',[10 185 100 22], ...
	    'String','C (uf/cm2)', ...
	    'Style','text');        
    GUIvar.cm = uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[120 185 85 22], ...
        'string',num2str(Temppara.cm),...
	    'Style','edit');
    uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',FG_Backgournd, ...
	    'HorizontalAlignment','right', ...
	    'ListboxTop',0, ...
	    'Position',[10 160 100 22], ...
	    'String','Rm (kohm-cm2)', ...
	    'Style','text');        
%    if isempty(zeng2.var{varnumber}.Ilk)   %Defined in Insert
	if ~isfield(zeng2.var{varnumber},'Ilk')%Defined in Insert
	    GUIvar.rm = uicontrol('Parent',GUIvar.figure, ...
	        'BackgroundColor',[1 1 1], ...
	        'ListboxTop',0, ...
	        'Position',[120 160 85 22], ...
            'string',num2str(Temppara.rm),...
	        'Style','edit', ...
	        'Tag','EditText1');
    else
	    GUIvar.rm = uicontrol('Parent',GUIvar.figure, ...
            'BackgroundColor',[1 1 1], ...
            'enable','off',...
	        'ListboxTop',0, ...
	        'Position',[120 160 85 22], ...
            'string',['1/G(' Temppara.model.I{zeng2.var{varnumber}.Ilk}.name ')'],...
	        'Style','edit', ...
	        'Tag','EditText1');
    end
    uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',FG_Backgournd, ...
	    'HorizontalAlignment','right', ...
	    'ListboxTop',0, ...
	    'Position',[10 135 100 22], ...
	    'String','Ra (ohm-cm)', ...
	    'Style','text');        
    GUIvar.ra = uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[120 135 85 22], ...
        'string',num2str(Temppara.ra),...
	    'Style','edit');
    uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',FG_Backgournd, ...
	    'HorizontalAlignment','right', ...
	    'ListboxTop',0, ...
	    'Position',[10 110 100 22], ...
	    'String','Vinitial (mV)', ...
	    'Style','text');        
    GUIvar.vini = uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[120 110 85 22], ...
        'string',num2str(Temppara.vini),...
	    'Style','edit');
    if ~isempty(Temppara.xyzi) 
        if size(Temppara.xyzi,1)~=1 | size(Temppara.xyzi,2)~=3
            zexst('err',[zeng2.dummyvar{i}.name 'is bad.'   char(13) 'xyzi has to be either EMPTY or in this format [x y z]'])
            return
        end
    end
    if ~isempty(Temppara.xyzc) 
        if size(Temppara.xyzc,1)~=1 | size(Temppara.xyzc,2)~=3
            zexst('err',[zeng2.dummyvar{i}.name 'is bad.'   char(13) 'xyzc has to be either EMPTY or in this format [x y z]'])
            return
        end
    end

    if zeng2.options.vestim==1 %'calculated'
        xyzenable='on';
    elseif zeng2.options.vestim==2 %'imported'
        xyzenable='off';
    else
        zexst('err','Errorin GUI_var')
    end

    uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',FG_Backgournd, ...
	    'HorizontalAlignment','right', ...
	    'ListboxTop',0, ...
	    'Position',[10 85 100 22], ...
	    'String','XYZi (vector)', ...
	    'Style','text');        
    GUIvar.xyzi = uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[120 85 85 22], ...
        'string',num2str(Temppara.xyzi),...
        'tag','xyz',...
        'enable',xyzenable,...
	    'Style','edit');
    uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',FG_Backgournd, ...
	    'HorizontalAlignment','right', ...
	    'ListboxTop',0, ...
	    'Position',[10 50 100 32], ...
	    'String',['XYZc (um)    ' char(10) '[center node]'], ...
	    'Style','text');
    GUIvar.xyzc = uicontrol('Parent',GUIvar.figure, ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[120 60 85 22], ...
        'string',num2str(Temppara.xyzc),...
        'tag','xyz',...
        'enable',xyzenable,...
	    'Style','edit');
    set(GUIvar.figure,'userdata',GUIvar)
    gui_var('update',GUIvar.figure,1);
case 'update Lmode'
    if nargin==1
        GUIvar=get(gcbf,'userdata');
    else
        GUIvar=get(varargin{2},'userdata');
    end
    varnumber=get(GUIvar.var,'value');
    temp=find(zeng2.var{varnumber}.name=='{');
    if isempty(temp)
        eval(['global ' zeng2.var{varnumber}.name])
    else
        eval(['global ' zeng2.var{varnumber}.name(1:(temp-1))])
    end
    eval(['Temppara=' zeng2.var{varnumber}.name ';'])
    if nargin==1
        Temppara.Lmode=get(gcbo,'value');
    else
        Temppara.Lmode=varargin{3};
    end
    if Temppara.Lmode==1
        %1=fix, 2=n.D(L/D)
        set(GUIvar.L,'enable','on')
        set(GUIvar.Linter_D,'enable','off')
    else
%        nseg=str2num(get(GUIvar.nseg,'string'));
%        Dia=str2num(get(GUIvar.dia,'string'));
%        Linter=str2num(get(GUIvar.Linter_D,'string'));
%        set(GUIvar.L,'string',num2str(nseg*Dia*Linter));
        set(GUIvar.L,'enable','off');
        set(GUIvar.Linter_D,'enable','on')
    end
    
case 'update number'
    gui_var('update',gcbf,get(gcbo,'value'));
    
case 'update'
    GUIvar=get(varargin{2},'userdata');
    varnumber=varargin{3};
    var_string=cell(1,length(zeng2.var));
    for i=1:length(zeng2.var)
        var_string{i}=zeng2.var{i}.name;
    end
    
    temp=find(zeng2.var{varnumber}.name=='{');
    if isempty(temp)
        eval(['global ' zeng2.var{varnumber}.name])
    else
        eval(['global ' zeng2.var{varnumber}.name(1:(temp-1))])
    end
    eval(['Temppara=' zeng2.var{varnumber}.name ';'])


    set(GUIvar.var,   'string',var_string,'value',varnumber);
    set(GUIvar.nseg,  'string',num2str(Temppara.nseg));
    set(GUIvar.dia,   'string',num2str(Temppara.dia));
    set(GUIvar.da_D,'string',num2str(Temppara.da_D));
    set(GUIvar.dn_D,'string',num2str(Temppara.dn_D));
    set(GUIvar.L,     'string',num2str(Temppara.L));
    set(GUIvar.Lmode, 'value',Temppara.Lmode);
    if Temppara.Lmode==1
        %1=fix, 2=n.D(L/D)
        set(GUIvar.L,'enable','on')
        set(GUIvar.Linter_D,'enable','off')
    else
        set(GUIvar.L,'enable','off')
        set(GUIvar.Linter_D,'enable','on')
        
    end
    set(GUIvar.Linter_D,'string',num2str(Temppara.Linter_D))
    set(GUIvar.lnodal,'string',num2str(Temppara.lnodal));
    set(GUIvar.cm,     'string',num2str(Temppara.cm));
    %    if isempty(zeng2.var{varnumber}.Ilk)   %Defined in Insert
	 if ~isfield(zeng2.var{varnumber},'Ilk')%Defined in Insert
       set(GUIvar.rm,'string',num2str(Temppara.rm),'enable','on');
    else
       set(GUIvar.rm,'string',['1/G(' Temppara.model.I{zeng2.var{varnumber}.Ilk}.name ')'],'enable','off');
    end
    set(GUIvar.ra,    'string',num2str(Temppara.ra));
    set(GUIvar.vini, 'string',num2str(Temppara.vini));
    
    if ~isempty(Temppara.xyzi) 
        if size(Temppara.xyzi,1)~=1 | size(Temppara.xyzi,2)~=3
            zexst('err',[zeng2.dummyvar{i}.name 'is bad.'   char(13) 'xyzi has to be either EMPTY or in this format [x y z]'])
            return
        end
    end
    if ~isempty(Temppara.xyzc) 
        if size(Temppara.xyzc,1)~=1 | size(Temppara.xyzc,2)~=3
            zexst('err',[zeng2.dummyvar{i}.name 'is bad.'   char(13) 'xyzc has to be either EMPTY or in this format [x y z]'])
            return
        end
    end
    set(GUIvar.xyzi,'string',num2str(Temppara.xyzi));
    set(GUIvar.xyzc,'string',num2str(Temppara.xyzc));
    gui_var('update Lmode',GUIvar.figure,Temppara.Lmode)
case 'check value'
    GUIvar=get(gcbf,'userdata');
    varnumber=get(GUIvar.var,'value');
    varargout{1}=0;
    temp=find(zeng2.var{varnumber}.name=='{');
    if isempty(temp)
        eval(['global ' zeng2.var{varnumber}.name])
    else
        eval(['global ' zeng2.var{varnumber}.name(1:(temp-1))])
    end

    temp=str2num(get(GUIvar.nseg,'string'));
    if isempty(temp)| ~isreal(temp)
        zexst('err',['Please give a reasonable value for nseg']);
        return
    else
        eval([zeng2.var{varnumber}.name '.nseg =temp;']);
        nseg=temp;
    end
    
    temp=str2num(get(GUIvar.dia,'string'));
    if isempty(temp)| ~isreal(temp)
        zexst('err',['Please give a reasonable value for dia']);
        return
    else
        eval([zeng2.var{varnumber}.name '.dia=temp;']);
        Dia=temp;    
    end


    temp=str2num(get(GUIvar.da_D,'string'));
    if ~isreal(temp)
        zexst('err',['Please give a reasonable value for da_D']);
        return
    else
        eval([zeng2.var{varnumber}.name '.da_D=temp;']);
    end
    
    temp=str2num(get(GUIvar.lnodal,'string'));
    if ~isreal(temp)
        zexst('err',['Please give a reasonable value for lnodal']);
        return
    else
        eval([zeng2.var{varnumber}.name '.lnodal=temp;']);
    end

    temp=str2num(get(GUIvar.dn_D,'string'));
    if ~isreal(temp)
        zexst('err',['Please give a reasonable value for dn_D']);
        return
    else
        eval([zeng2.var{varnumber}.name '.dn_D=temp;']);
    end
    
    Lmode=get(GUIvar.Lmode,'value');%1=fix 2=varied
    eval([zeng2.var{varnumber}.name '.Lmode=Lmode;']);;

    if Lmode==1
        temp=str2num(get(GUIvar.L,'string'));
        if isempty(temp)| ~isreal(temp)
            zexst('err',['Please give a reasonable value for L']);
            return
        else
            eval([zeng2.var{varnumber}.name '.L=temp;']);;
        end
    else
        temp=str2num(get(GUIvar.Linter_D,'string'));
        if isempty(temp)| ~isreal(temp)
            zexst('err',['Please give a reasonable value for Linter/Dia']);
            return
        else
            eval([zeng2.var{varnumber}.name '.Linter_D=temp;']);;
            L=temp*Dia*nseg;
            eval([zeng2.var{varnumber}.name '.L=L;']);;
            set(GUIvar.L,'string',num2str(L))
        end
    end
    
    temp=str2num(get(GUIvar.cm,'string'));
    if isempty(temp)| ~isreal(temp)
        zexst('err',['Please give a reasonable value for cm']);
        return
    else
        eval([zeng2.var{varnumber}.name '.cm=temp;']);
    end
    

    if strcmp('on',get(GUIvar.rm,'enable'))
        temp=str2num(get(GUIvar.rm,'string'));
        if isempty(temp)| ~isreal(temp)
            zexst('err',['Please give a reasonable value for rm']);
            return
        else
            eval([zeng2.var{varnumber}.name '.rm=temp;']);
        end
    end

    temp=str2num(get(GUIvar.ra,'string'));
    if isempty(temp)| ~isreal(temp)
        zexst('err',['Please give a reasonable value for ra']);
        return
    else
        eval([zeng2.var{varnumber}.name '.ra=temp;']);
    end
    
    temp=str2num(get(GUIvar.vini,'string'));
    if isempty(temp)| ~isreal(temp)
        zexst('err',['Please give a reasonable value for vini']);
        return
    else
        eval([zeng2.var{varnumber}.name '.vini=temp;']);
    end
    
    
    
    xyzi=str2num(get(GUIvar.xyzi,'string'));
    xyzc=str2num(get(GUIvar.xyzc,'string'));

    if isempty(xyzi) & isempty(xyzc)
        eval([zeng2.var{varnumber}.name '.xyzi=[];']);
        eval([zeng2.var{varnumber}.name '.xyzc=[];']);
    else
        if size(xyzi,1)~=1 | size(xyzi,2)~=3
            zexst('err',['XYZi has to be either EMPTY or in this format [x y z]'])
            return
        else
            eval([zeng2.var{varnumber}.name '.xyzi=xyzi;']);
        end
        if size(xyzc,1)~=1 | size(xyzc,2)~=3
            zexst('err',['XYZc has to be either EMPTY or in this format [x y z]'])
            return
        else
            eval([zeng2.var{varnumber}.name '.xyzc=xyzc;']);
        end
    end
    varargout{1}=1;
case 'okay'
    if gui_var('check value');
    	if nargin==1
        else        
            %from apply
            zexst('calculate')
        end
    end
  
end
