No BSD License  

Highlights from
Neurocal

image thumbnail
from Neurocal by Zeng Lertmanorat
Simulation describing the electrical activity of nerve cell (neuron) by solving cable equation

Find_Ith_GUI(varargin)
function varargout = Find_Ith_GUI(varargin)
global zeng zeng2
switch varargin{1}
case 'initial'
    Parent_Size=get(varargin{2},'position');
    FG_Size=[Parent_Size(1) Parent_Size(2)-360-30  170 360];
    ScreenSize=get(0,'ScreenSize');
    if FG_Size(2)<100;
        FG_Size(2)=Parent_Size(2)+Parent_Size(4)+45;
    end
    temp=findobj('type','figure','tag','ZengAnalysis');
    if ~isempty(temp)
        set(temp,'position',FG_Size);
        figure(temp)
    else
        %model setup-----------------------------------------------------
        zexst('open model file','FindIth_Iext_axon.m');
        %/model setup-----------------------------------------------------
        zeng2.options.vestim=1; %1=calculated, 2=importedf
        zexst('change options','vestim',1);
        zexst('DeleteFcn')
        %------------------------------------
        zeng.dtmode=1;
        zeng.dt=0.5;
        %------------------------------------
        ZengIth.figure = figure('Color',[0.8 0.8 0.8], ...
        'MenuBar','none',...
        'Name','Find Ith',...
        'NumberTitle','off',...
    	'Position',FG_Size, ... %%%%'ResizeFcn','Find_Ith_GUI(''ResizeFcn'');',...
        'tag','ZengAnalysis_Find_Ith_GUI',...
    	'ToolBar','none');
        set(ZengIth.figure,'position',FG_Size)
        uicontrol('Parent',ZengIth.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[0.8 0.8 0.8], ...
	    'ListboxTop',0, ...
	    'Position',[10 325 60 25], ...
        'String','Step(us)', ...
        'style','text');
        ZengIth.edit.dt=uicontrol('Parent',ZengIth.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[75 325 80 25], ...
        'String',num2str(zeng.dt), ...
        'style','edit');
        uicontrol('Parent',ZengIth.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[0.8 0.8 0.8], ...
	    'ListboxTop',0, ...
	    'Position',[10 290 60 25], ...
        'String','Start (us)', ...
        'style','text');
        ZengIth.edit.start=uicontrol('Parent',ZengIth.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[75 290 80 25], ...
        'String',num2str(100), ...
        'style','edit');
        uicontrol('Parent',ZengIth.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[0.8 0.8 0.8], ...
	    'ListboxTop',0, ...
	    'Position',[10 255 60 25], ...
        'String','Stop (us)', ...
        'style','text');
        ZengIth.edit.stop=uicontrol('Parent',ZengIth.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[75 255 80 25], ...
        'String',num2str(400), ...
        'style','edit');
        uicontrol('Parent',ZengIth.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[0.8 0.8 0.8], ...
	    'ListboxTop',0, ...
	    'Position',[10 220 60 25], ...
        'String','Threshold (mV)', ...
        'style','text');
        ZengIth.edit.Threshold=uicontrol('Parent',ZengIth.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[75 220 80 25], ...
        'String',num2str(75), ...
        'style','edit');
        uicontrol('Parent',ZengIth.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[0.8 0.8 0.8], ...
	    'ListboxTop',0, ...
	    'Position',[10 185 60 25], ...
        'String','Ith Err%', ...
        'style','text');
        ZengIth.edit.Ierr=uicontrol('Parent',ZengIth.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[75 185 80 25], ...
        'String',num2str(0.2), ...
        'style','edit');
        uicontrol('Parent',ZengIth.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[0.8 0.8 0.8], ...
	    'ListboxTop',0, ...
	    'Position',[10 150 140 25], ...
        'String','Dmin      Dmax       #Dia', ...
        'style','text');
        ZengIth.edit.Dia(1)=uicontrol('Parent',ZengIth.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[10 115 47 25], ...
        'String','2', ...
        'style','edit');
        ZengIth.edit.Dia(2)=uicontrol('Parent',ZengIth.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[59 115 47 25], ...
        'String','20', ...
        'style','edit');
        ZengIth.edit.Dia(3)=uicontrol('Parent',ZengIth.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[108 115 47 25], ...
        'String','19', ...
        'style','edit');
        uicontrol('Parent',ZengIth.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[0.8 0.8 0.8], ...
	    'ListboxTop',0, ...
	    'Position',[10 75 15 25], ...
        'String','#X', ...
        'style','text');
        ZengIth.edit.num_x=uicontrol('Parent',ZengIth.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[30 80 40 25], ...
        'String','5', ...
        'style','edit');
        ZengIth.pop.x=uicontrol('Parent',ZengIth.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[75 80 80 25], ...
        'String','0 - L|0 - L/2|Random', ...
        'style','popupmenu',...
        'value',1);
        ZengIth.check.random_z=uicontrol('Parent',ZengIth.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[0.8 0.8 0.8], ...
	    'ListboxTop',0, ...
	    'Position',[15 45 100 25], ...
        'String','Rand Z(um)', ...
        'style','check',...
        'value',1);
        ZengIth.edit.random_z=uicontrol('Parent',ZengIth.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[95 45 60 25], ...
        'String','50 300', ...
        'style','edit');
        uicontrol('Parent',ZengIth.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[0.85 0.8 0.75], ...
        'callback','Find_Ith_GUI(''Apply'',gcf)',...
	    'ListboxTop',0, ...
	    'Position',[50 10 60 25], ...
        'String','Apply', ...
	    'Tag','Pushbutton');
    
%        ZengIth.menu.file(1)=uimenu('parent',ZengIth.figure,'label','File');
%        ZengIth.menu.file(2)=uimenu(ZengIth.menu.file(1),'callback','Find_Ith_GUI(''fired axons'')','label','Save fired axons');
        set(ZengIth.figure,'userdata',ZengIth)
    end

case 'Load Model'
    zexst('err','Sorry.. This version support only this model file.');
    
    
case 'ResizeFcn'
    FG_Size=get(gcbf,'position');
    FG_Size(3:4)=[max(FG_Size(3),250) max(FG_Size(4),250)];

case 'Apply'    
    %-----------------------------------------
    if length(zeng2.varlist)>1
        zexst('err','Sorry.. This version support only "axon" in the model file.');
        return
    else
        for tempzeng=1:length(zeng2.varlist)
            feval('global',zeng2.varlist{tempzeng})
        end
    end
    ZengIth=get(varargin{2},'userdata');   
    tic
    %------------------------------------
    temp=str2num(get(ZengIth.edit.dt ,'string'));
    if isempty(temp)| temp<=0 | ~isreal(temp) 
        zexst('err','Time step has to be greater than zero');
        return
    end
    zeng.dt=temp;
    temp=str2num(get(ZengIth.edit.start ,'string'));
    if isempty(temp)| temp<=0 | ~isreal(temp) 
        zexst('err','Start has to be a positive number.');
        return
    end
    Fired_para(1)=temp/1000; %[Detect_Start Detect_Threthold  ]
    temp=str2num(get(ZengIth.edit.stop ,'string'))/1000;
    if isempty(temp)| temp<=0 | ~isreal(temp) 
        zexst('err','Stop has to be a positive number.');
        return
    elseif temp<=(Fired_para(1))
        zexst('err','Stop has to be greater than Start')
        return
    end
    zeng.tf=temp;    
    temp=str2num(get(ZengIth.edit.Threshold ,'string'));
    if isempty(temp)| temp<=0 | ~isreal(temp) 
        zexst('err','Threshold has to be a positive number.');
        return
    end

    eval(['Fired_para(2)=temp+' zeng2.var{1}.name '.vini;']) %[Detect_Start Detect_Threthold]
    temp=str2num(get(ZengIth.edit.Ierr ,'string'));
    if isempty(temp) | ~isreal(temp) | abs(temp)>5
        zexst('err','Ith-%Err should not be more than 5..');
        return
    end
    ZengIth.cal.Ierr=abs(temp);
    for i=1:3
	    Temp=str2num(get(ZengIth.edit.Dia(i),'string'));
	    if isempty(Temp)| ~isreal(Temp)
   	        zext('err',['I prefer a number for Diameter-' num2str(i)  ', You know?']);
	        return
   	    else
      	    Dia(i) =[Temp];
        end
    end
    if Dia(1)>Dia(2)
        zext('err','Dmin is larger than Dmax.');
        return
    end
    if Dia(3)==1;
    	ZengIth.cal.Dia=Dia(1);
    else
     	ZengIth.cal.Dia=linspace(Dia(1),Dia(2),Dia(3));
    end
    ZengIth.cal.Dia_length=length(ZengIth.cal.Dia);
    temp=round(str2num(get(ZengIth.edit.num_x ,'string')));
    if isempty(temp) | ~isreal(temp) | temp<1
        zexst('err','#x has to be positive integer.');
        return
    end
    ZengIth.cal.X_number=temp;
    ZengIth.cal.X=zeros(ZengIth.cal.X_number,ZengIth.cal.Dia_length);
    temp=get(ZengIth.pop.x,'value');
    %'0-L|0-L/2|Random'
    if temp==1 %systematic-L
        ZengIth.cal.X(:,1)=linspace(0,1,ZengIth.cal.X_number)';
        for i=2:ZengIth.cal.Dia_length
            ZengIth.cal.X(:,i)=ZengIth.cal.X(:,1);
        end
    elseif temp==2 %%systematic-L/2
        ZengIth.cal.X(:,1)=linspace(0,0.5,ZengIth.cal.X_number)';
        for i=2:ZengIth.cal.Dia_length
            ZengIth.cal.X(:,i)=ZengIth.cal.X(:,1);
        end
    else %random
        ZengIth.cal.X=rand(ZengIth.cal.X_number,ZengIth.cal.Dia_length)*2-1; %[-1 +1]
    end
    eval(['Linter_D=' zeng2.var{1}.name '.Linter_D;'])
    for i=1:ZengIth.cal.Dia_length
        ZengIth.cal.X(:,i)=ZengIth.cal.X(:,i)*Linter_D*ZengIth.cal.Dia(i);
    end
    %Don't sort(X), do sort(Z) only.
    if get(ZengIth.check.random_z,'value')
        temp=str2num(get(ZengIth.edit.random_z ,'string'));
        if isempty(temp) | ~isreal(temp) | length(temp)~=2
            zexst('err','Random-Z has to range(min max) of Z.');
            return
        elseif temp(1)>temp(2)
            zexst('err','Random-Zmin > Random-max is not possible.');
            return
        end
        ZengIth.cal.Z=rand(ZengIth.cal.X_number,ZengIth.cal.Dia_length)*diff(temp)+temp(1);
        ZengIth.cal.Z=sort(ZengIth.cal.Z);
    else
        ZengIth.cal.Z=[];
    end
    ZengIth.cal.Ith=zeros(ZengIth.cal.X_number,ZengIth.cal.Dia_length);
    Iexstim_original=zeng.Iexstim;
    temp=0;
    Iexstim_n=length(zeng.Iexstim);
    for k=1:Iexstim_n
        temp=max(temp,abs(zeng.Iexstim(k).amp));
    end
    for k=1:Iexstim_n
        zeng.Iexstim(k).amp=zeng.Iexstim(k).amp/temp;
    end
    Iexstim=zeng.Iexstim;
    Iminmaxmean=615;
    Total_axons=num2str(ZengIth.cal.Dia_length*ZengIth.cal.X_number);
    Total_axons_i=0;
    for i=1:ZengIth.cal.Dia_length
        eval([zeng2.var{1}.name '.dia =ZengIth.cal.Dia(i);'])
        for j=1:ZengIth.cal.X_number
            Total_axons_i=Total_axons_i+1;
            disp([num2str(Total_axons_i) '/' Total_axons])           
            Iminmax(1:2)=Iminmaxmean;
            for k=1:Iexstim_n
                zeng.Iexstim(k).amp   =Iexstim(k).amp*Iminmaxmean;
                zeng.Iexstim(k).xyz(1)=Iexstim(k).xyz(1)+ZengIth.cal.X(j,i);
                if ~isempty(ZengIth.cal.Z)
                    zeng.Iexstim(k).xyz(3)=ZengIth.cal.Z(j,i);
                end
            end
            [Fired]=zexst('calculate',Fired_para); 
%            disp(['start with I = '   num2str([Iminmax]) ' - ' num2str(Fired)])
            if Fired==-1
                return
            elseif Fired==0
                while Fired==0
                    Iminmax(1)=Iminmax(2);
	                Iminmax(2)=Iminmax(2)*2;
                    for k=1:Iexstim_n
                        zeng.Iexstim(k).amp=Iminmax(2)*Iexstim(k).amp;
                    end
                    [Fired]=zexst('calculate',Fired_para); 
                end
            elseif Fired==1
                while Fired==1
                    Iminmax(2)=Iminmax(1);
                    Iminmax(1)=Iminmax(1)*0.75;
                    for k=1:Iexstim_n
                        zeng.Iexstim(k).amp=Iminmax(1)*Iexstim(k).amp;
                    end
                    [Fired]=zexst('calculate',Fired_para); 
                end
            else
                disp('err')
                return
            end
%            disp(['I found you at ' num2str(Iminmax)])
            while 1
                Iminmaxmean=mean(Iminmax);
                for k=1:Iexstim_n
                    zeng.Iexstim(k).amp=Iminmaxmean*Iexstim(k).amp;
                end
                [Fired]=zexst('calculate',Fired_para); 
                
%                disp(['           I = '   num2str([Iminmaxmean]) ' - ' num2str(Fired)])
                if Fired==-1
                    return
                elseif Fired==1
                    if (100*(Iminmaxmean-Iminmax(1))/Iminmaxmean)>ZengIth.cal.Ierr
                        Iminmax(2)=Iminmaxmean;
                    else
                        disp(['           I = '   num2str([Iminmaxmean])])
                        ZengIth.cal.Ith(j,i)=Iminmaxmean;
                        break
                    end
                elseif Fired==0
                    if (100*(Iminmax(2)-Iminmaxmean)/Iminmaxmean)>ZengIth.cal.Ierr
                        Iminmax(1)=Iminmaxmean;
                    else
                        disp(['           I = '   num2str([Iminmax(2)])])
                        ZengIth.cal.Ith(j,i)=Iminmax(2);
                        break
                    end
                end
            end
            
        end %for j=1:ZengIth.cal.X_number
        set(ZengIth.figure,'userdata',ZengIth);
        Find_Ith_GUI('Save Threshold',ZengIth.figure)
    end% for i=1:ZengIth.cal.Dia_length
    zeng.Iexstim=Iexstim_original;
    set(ZengIth.figure,'userdata',ZengIth);
    temp=toc;
    Find_Ith_GUI('Save Threshold',ZengIth.figure)
    figure    
    if size(ZengIth.cal.Ith,1)==1
      plot(ZengIth.cal.Dia,ZengIth.cal.Ith);
    else
        errorbar(ZengIth.cal.Dia,mean(ZengIth.cal.Ith),std(ZengIth.cal.Ith),'b');
    end
       
    
    if temp<=60
        disp(['finish run with ' num2str(temp) ' seconds' ])
    elseif temp<=3600
        temp_minute=fix(temp/60);
        temp_second=mod(temp,60);
        disp(['finish run with ' num2str(temp_minute) ':' num2str(temp_second) ' min:sec' ])
    else
        temp_hour=fix(temp/3600);
        temp=temp-temp_hour*3600;
        temp_minute=fix(temp/60);
        temp_second=mod(temp,60);
        disp(['finish run with ' num2str(temp_hour) ':' num2str(temp_minute) ':' num2str(temp_second) ' h:m:sec' ])
    end
case 'Save Threshold'
    ZengIth=get(varargin{2},'userdata');
    if 1
        ZengIth.Filename=[zeng2.file(1:(length(zeng2.file)-2)) '_Z-50-300'  '_PW-' num2str(1000*zeng.Iexstim(1).dur) '_dt-' num2str(zeng.dt) '_D2-20.thr'];
        ZengIth.Path=[zeng2.cd '\'];
    else
        [Filename Path]=uiputfile([thr.cd '\*.thr']);
    end
    if ~strcmp('0',num2str(ZengIth.Filename)) & ~strcmp('0',num2str(ZengIth.Path));
        Have_dot=find(ZengIth.Filename=='.');
        if isempty(Have_dot)
      	    ZengIth.Filename=[ZengIth.Filename '.thr'];   
        end
        dlmwrite([ZengIth.Path ZengIth.Filename],'');
        FID=fopen([ZengIth.Path ZengIth.Filename],'w');
        fprintf(FID,['I(mA) \t']);
        for k=1:length(zeng.Iexstim)
            fprintf(FID,[num2str(zeng.Iexstim(k).amp) '\t' ]);
        end
        fprintf(FID,['\n PW(us) \t']);
        for k=1:length(zeng.Iexstim)
            fprintf(FID,[num2str(zeng.Iexstim(k).dur*1000) '\t' ]);
        end
        fprintf(FID,['\n Delay(us) \t']);
        for k=1:length(zeng.Iexstim)
            fprintf(FID,[num2str(zeng.Iexstim(k).delay*1000) '\t' ]);
        end
        fprintf(FID,['\n X \t']);
        for k=1:length(zeng.Iexstim)
            fprintf(FID,[num2str(zeng.Iexstim(k).xyz(1)) '\t' ]);
        end
        fprintf(FID,['\n Y \t']);
        for k=1:length(zeng.Iexstim)
            fprintf(FID,[num2str(zeng.Iexstim(k).xyz(2)) '\t' ]);
        end
        fprintf(FID,['\n Z \t']);
        for k=1:length(zeng.Iexstim)
            fprintf(FID,[num2str(zeng.Iexstim(k).xyz(3)) '\t' ]);
        end
        fprintf(FID,['\n Time step (us) \t']);
        fprintf(FID,[num2str(zeng.dt)]);
        fprintf(FID,['\n']);
        for i=1:ZengIth.cal.Dia_length
		    fprintf(FID,[num2str(ZengIth.cal.Dia(i)) '\t' ]);
        end
        fprintf(FID,['\t']);
        fprintf(FID,['\t' 'X']);
        for i=1:ZengIth.cal.Dia_length
		    fprintf(FID,[ '\t' ]);
        end
        fprintf(FID,['\t' 'Z']);
        fprintf(FID,['\n']);
        for j=1:ZengIth.cal.X_number
            for i=1:ZengIth.cal.Dia_length
			    fprintf(FID,[num2str(ZengIth.cal.Ith(j,i)) '\t' ]);
            end
            fprintf(FID,['\t']);
            for i=1:ZengIth.cal.Dia_length
			    fprintf(FID,[num2str(ZengIth.cal.X(j,i)) '\t' ]);
            end
            fprintf(FID,['\t']);
            if isempty(ZengIth.cal.Z)
            else
                for i=1:ZengIth.cal.Dia_length
			        fprintf(FID,[num2str(ZengIth.cal.Z(j,i)) '\t' ]);
                end
            end
            fprintf(FID,['\n']);
        end
        Status=fclose(FID);
    end
end

Contact us at files@mathworks.com