function create_StimVe
global zeng zeng2
Iexstim_length=length(zeng.Iexstim);
if Iexstim_length==0 
    %the empty value represents "no stim"
    zeng2.setup.stimVe=[];
    return
end
zeng2.setup.stimVe.time     =zeros(Iexstim_length,2);
zeng2.setup.stimVe.vestim   =zeros(zeng2.setup.nseg_total,Iexstim_length); %one column for one stim
zeng2.setup.stimVe.WaveformIndex=[];
if zeng2.options.vestim==1 %'calculated'
    %V = IR/(4.pi.r)
    for i=1:Iexstim_length
        if isnumeric(zeng.Iexstim(i).amp)
            zeng2.setup.stimVe.time(i,:)  = zeng.Iexstim(i).delay + [0 zeng.Iexstim(i).dur];
        else
            zeng2.setup.stimVe.WaveformIndex=[zeng2.setup.stimVe.WaveformIndex i];
            %the time is not set, but equal to zero by default.
            %the zero time represents no stim.
            %zeng2.setup.stimVe.time(i,:)  = 0;
        end
        for j=1:length(zeng2.dummyvar)
            if isempty(zeng2.dummyvar{j}.xyz)
                zexst('err',['Err in Create_StimVe' char(13) 'xyz is not assigned to ' zeng2.dummyvar{j}.name])
                zeng2.notreadytorun=1;
                return
                
            elseif length(zeng2.dummyvar{j}.xyz(:,1))~=zeng2.dummyvar{j}.nseg
                zexst('err',['Err in Create_StimVe' char(13) 'sizes of nseg and xyz do not match for ' zeng2.dummyvar{j}.name])
                zeng2.notreadytorun=1;
                return
            else
                %convert unit of r from um to cm;    
                r=1/10000*sqrt( (zeng2.dummyvar{j}.xyz(:,1)-zeng.Iexstim(i).xyz(1)).^2 + (zeng2.dummyvar{j}.xyz(:,2)-zeng.Iexstim(i).xyz(2)).^2  + (zeng2.dummyvar{j}.xyz(:,3)-zeng.Iexstim(i).xyz(3)).^2);
                if isnumeric(zeng.Iexstim(i).amp)
                    zeng2.setup.stimVe.vestim(zeng2.setup.nseg_length(j,1):zeng2.setup.nseg_length(j,2),i)=zeng.Iexstim(i).amp*zeng2.amp*zeng.re/4/pi./r;
                else
                    zeng2.setup.stimVe.vestim(zeng2.setup.nseg_length(j,1):zeng2.setup.nseg_length(j,2),i)=                    zeng2.amp*zeng.re/4/pi./r; %1uA
                end
            end
        end %for j=1:length(zeng2.dummyvar)
    end %for i=1:Iexstim_length
    zeng2.setup.stimVe.vestim=zeng2.setup.stimVe.vestim/1000; %uV->mV
elseif zeng2.options.vestim==2 %imported
    %V is manualed assigned
    %Iexstim_length has to be equal to size(zeng2.dummyvar{j}.vestim,2)
    %for setting the amp, dur, and delay
    for i=1:Iexstim_length
        if isnumeric(zeng.Iexstim(i).amp)
            zeng2.setup.stimVe.time(i,:)  = zeng.Iexstim(i).delay + [0 zeng.Iexstim(i).dur];
        else
            zeng2.setup.stimVe.WaveformIndex=[zeng2.setup.stimVe.WaveformIndex i];
            %the time is not set, but equal to zero by default.
            %the zero time represents no stim.
            %zeng2.setup.stimVe.time(i,:)  = 0;
        end
        for j=1:length(zeng2.dummyvar)
            if size(zeng2.dummyvar{j}.vestim,1)~=zeng2.dummyvar{j}.nseg
                zexst('err',['Err in Create_StimVe' char(13) 'For the imported Ve,' char(13) ' nseg and vestim for ' zeng2.dummyvar{j}.name ' need to be equal in size.'])
                zeng2.notreadytorun=1;
                return
            elseif size(zeng2.dummyvar{j}.vestim,2)~=Iexstim_length
                zexst('err',['Err in Create_StimVe' char(13) 'For the imported Ve,' char(13) 'number of Vestims in the model and Ve assign to' zeng2.dummyvar{j}.name ' are not equal in size.'])
                zeng2.notreadytorun=1;
                return
            else				
                if isnumeric(zeng.Iexstim(i).amp)
                    zeng2.setup.stimVe.vestim(zeng2.setup.nseg_length(j,1):zeng2.setup.nseg_length(j,2),i)=zeng.Iexstim(i).amp*zeng2.amp*zeng2.dummyvar{j}.vestim(:,i);
                else
                    zeng2.setup.stimVe.vestim(zeng2.setup.nseg_length(j,1):zeng2.setup.nseg_length(j,2),i)=                    zeng2.amp*zeng2.dummyvar{j}.vestim(:,i); %1uA
                end
            end
        end
    end
else
    zexst('error','Error in Create_stimVe');
    zeng2.notreadytorun=1;
end
%---------------------------------------------------
temp=1:Iexstim_length;
temp(zeng2.setup.stimVe.WaveformIndex)=[];
zeng2.setup.stimVe.timerange=[min(zeng2.setup.stimVe.time(temp,1)) max(zeng2.setup.stimVe.time(temp,2))];
%---------------------------------------------------
if isempty(zeng2.setup.stimVe.timerange)
    %This happens when these is only Iamp(t) stim (no square pulse stim) in the model.
    zeng2.setup.stimVe.timerange=[0 0];
end