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

Ansoft2FEA(varargin)
function varargout = Ansoft2FEA(varargin)
if nargin==0
    action='initial';
else
    action=varargin{1};
end
switch action
case 'initial'
    ScreenSize=get(0,'ScreenSize');
    FG_Size=[(ScreenSize(3)-500)/2 (ScreenSize(4)-360)/2  500 360];
    FEA.Filename=[];
    FEA.yz=[];
    FEA.figure = figure('Color',[0.8 0.8 0.8], ...
        'MenuBar','none',...
        'Name','Ansoft2FEA',...
        'NumberTitle','off',...
    	'Position',FG_Size, ...
        'ResizeFcn','Ansoft2FEA(''ResizeFcn'')',... 
        'tag','ZengAnalysis',...
    	'ToolBar','none');
    set(FEA.figure,'position',FG_Size)
    uicontrol('Parent',FEA.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[0.85 0.8 0.75], ...
        'callback','Ansoft2FEA(''Load Dir'')',...
	    'ListboxTop',0, ...
	    'Position',[10 255 60 25], ...
        'String','Load Dir', ...
	    'Tag','Pushbutton');

    uicontrol('Parent',FEA.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[0.85 0.8 0.75], ...
        'callback','Ansoft2FEA(''Load FEM'')',...
	    'ListboxTop',0, ...
	    'Position',[10 225 60 25], ...
        'String','Load FEM', ...
	    'Tag','Pushbutton');
    FEA.text.FEM=uicontrol('Parent',FEA.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[75 225 80 25], ...
        'String','FEM file', ...
        'style','text');
    FEA.rad.no=uicontrol('Parent',FEA.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[0.8 0.8 0.8], ...
        'callback','Ansoft2FEA(''option'');',...
        'Position',[10 190 80 25], ...
        'String','No filter', ...
        'style','radio',...
        'value',1,...
        'tag','option');
    FEA.rad.round=uicontrol('Parent',FEA.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[0.8 0.8 0.8], ...
        'callback','Ansoft2FEA(''option'');',...
	    'Position',[10 150 60 25], ...
        'String','Round', ...
        'style','radio',...
        'tag','option');
    FEA.rad.box=uicontrol('Parent',FEA.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[0.8 0.8 0.8], ...
        'callback','Ansoft2FEA(''option'');',...
	    'Position',[75 150 60 25], ...
        'String','Box', ...
        'style','radio',...
        'tag','option');
    uicontrol('Parent',FEA.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[0.8 0.8 0.8], ...
	    'ListboxTop',0, ...
	    'Position',[10 115 60 25], ...
        'String','Radius (um)', ...
        'style','text');
    FEA.edit.r=uicontrol('Parent',FEA.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[75 115 80 25], ...
        'String','', ...
        'style','edit');
    uicontrol('Parent',FEA.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[0.8 0.8 0.8], ...
	    'ListboxTop',0, ...
	    'Position',[10 80 60 25], ...
        'String','Ymean (um)', ...
        'style','text');
    FEA.edit.y=uicontrol('Parent',FEA.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[75 80 80 25], ...
        'String','', ...
        'style','edit');
    uicontrol('Parent',FEA.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[0.8 0.8 0.8], ...
	    'ListboxTop',0, ...
	    'Position',[10 45 60 25], ...
        'String','Zmean (um)', ...
        'style','text');
    FEA.edit.z=uicontrol('Parent',FEA.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[1 1 1], ...
	    'ListboxTop',0, ...
	    'Position',[75 45 80 25], ...
        'String','', ...
        'style','edit');
    uicontrol('Parent',FEA.figure, ...
        'Units','pixel', ...
	    'BackgroundColor',[0.85 0.8 0.75], ...
        'callback','Ansoft2FEA(''save'')',...
	    'ListboxTop',0, ...
	    'Position',[50 10 60 25], ...
        'String','Save', ...
	    'Tag','Pushbutton');
    
    FEA.axes=axes('Parent',FEA.figure, ...
        'Units','pixel', ...
	    'Position',[185 20 FG_Size(3)-195 FG_Size(4)-30],...
        'tag','axes');
    set(FEA.figure,'userdata',FEA)
case 'ResizeFcn'
    FEA=get(gcf,'userdata');
    FG_Size=get(FEA.figure,'position');
    axes_Size=get(FEA.axes,'position');
    set(FEA.axes,'Position',[axes_Size(1:2) max(200,FG_Size(3)-195) max(40,FG_Size(4)-30)])
case 'Load Dir'
    Path=uigetdir;
    
    if ~strcmp('0',num2str(Path));
        TempDir=dir(Path);
        Dir_Length=length(TempDir)-2;
        if Dir_Length<1
            return
        end
        for i=1:Dir_Length
            disp([num2str(i) '/' num2str(Dir_Length)])
            Ansoft2FEA('Load FEM','LoadDir',TempDir(i+2).name,[Path '\'])
            Ansoft2FEA('save','LoadDir',TempDir(i+2).name(1:(length(TempDir(i+2).name)-4)),[Path '\'])
        end
    end
case 'Load FEM'
    if nargin==1 %called from LoadFEM button
        [Filename Path]=uigetfile(['*.reg']);
    else
        if strcmp(varargin{2},'LoadDir')
            Filename=varargin{3};
            Path=varargin{4};
        else
            return
        end
    end
    if ~strcmp('0',num2str(Filename)) & ~strcmp('0',num2str(Path));
        h_msgbox=msgbox(['Please sit back and relax. ' char(13) 'It will take a while.']);
        FEA=get(gcbf,'userdata');
        %-----------------------------
        BadlineText='-NoSoln-';
        BadlineTextBlock=[1:length(BadlineText)]-1;
        Badlinetemp='12345678';
        %-------------------------------
        FEA.Path=Path;
        dot=find(Filename=='.');
      
        if isempty(dot)
            FEA.Filename=Filename;
        else
            FEA.Filename=Filename(1:(dot-1));
        end
        set(FEA.text.FEM,'string',FEA.Filename);
		tic      
        FID=fopen([Path Filename],'r');
        line=['zeng'];
        FEA.FEM=[];
        while isempty(str2num(line))
            Ftell=ftell(FID);
            line=fgetl(FID);
        end
        fseek(FID,Ftell,-1);
        S = fscanf(FID,'%c');
        %-------------------------------
        Nosolu=findstr(S,BadlineText);
        if ~isempty(Nosolu)
            for i=1:length(Nosolu)
                S([Nosolu(i)+BadlineTextBlock])=Badlinetemp;
            end
        end
        FEA.FEM=str2num(S);
        temp=find(FEA.FEM(:,4)==str2num(Badlinetemp));
        if length(temp)~=length(Nosolu)
            errordlg('Bad file')
            if ishandle(h_msgbox)
                delete(h_msgbox)
            end
            FEA.yz=0;
            FEA.FEM=0;
            return
        end
        FEA.FEM(temp,:)=[];
        %-------------------------------
        %Convert from mm to um, and V to mV
        FEA.FEM=FEA.FEM*1000;
        fclose(FID);
        %Find yz---------------------------------------------
        minmax=[min(FEA.FEM(:,1)) max(FEA.FEM(:,1))];
        xmax=find(FEA.FEM(:,1)==minmax(2));
        xmin=find(FEA.FEM(:,1)==minmax(1));
        xmean=mean(minmax);
        temp=min(abs(FEA.FEM(:,1)-xmean));
        xmean=find(abs(FEA.FEM(:,1)-xmean)==temp(1));
        if length(xmin)==length(xmax)==length(xmean)
            mychoice=xmin;
        else
            lengthmax=max([length(xmin) length(xmax) length(xmean)]);
            if length(xmin)==lengthmax
                mychoice=xmin;
            elseif length(xmax)==lengthmax
                mychoice=xmax;
            else
                mychoice=xmean;
            end
        end
        FEA.yz=FEA.FEM(mychoice,[2 3]);
        %/Find yz---------------------------------------------
        toc/60

        set(FEA.figure,'userdata',FEA)
        delete(get(FEA.axes,'children'))
        axes(FEA.axes)
        for i=1:length(FEA.yz)
      	    patch(FEA.yz(i,1),FEA.yz(i,2),'b',...
         		'marker','o',...
         		'LineWidth',1.25,...   
				'MarkerEdgeColor','b',...
				'MarkerFaceColor','none',...
      			'MarkerSize',8);
	    end
        ylim=[min(FEA.yz(:,1))-10 max(FEA.yz(:,1))+10];
        zlim=[min(FEA.yz(:,2))-10 max(FEA.yz(:,2))+10];
        axis([ylim zlim])
        %------------------------------------------
        if nargin==1 %called from LoadFEM button
            Ansoft2FEA('option',FEA.figure);
        end
        if ishandle(h_msgbox)
            delete(h_msgbox)
        end
    end
case 'option'
    if nargin==1
        set(findobj(gcbf,'tag','option'),'value',0)
        set(gcbo,'value',1)
        FEA=get(gcbf,'userdata');
    else
        FEA=get(varargin{2},'userdata');
    end
    if isempty(FEA.yz)
        return
    end
    ylim=[min(FEA.yz(:,1)) max(FEA.yz(:,1))];
    zlim=[min(FEA.yz(:,2)) max(FEA.yz(:,2))];
    %------------------------------------------
    if get(FEA.rad.round,'value')
        set(FEA.edit.r,'string',num2str([abs(diff(zlim)/2)]))
        set(FEA.edit.y,'string',num2str(mean(ylim)))
        set(FEA.edit.z,'string',num2str(mean(zlim)))
    elseif get(FEA.rad.box,'value')
        if abs(diff(ylim))>abs(diff(zlim))
            r=abs(diff(zlim)/2);
            set(FEA.edit.r,'string',num2str(r))
            set(FEA.edit.y,'string',num2str([ylim]+[r -r]))
            set(FEA.edit.z,'string',num2str(mean(zlim)))
        else
            r=abs(diff(ylim)/2);
            set(FEA.edit.r,'string',num2str(r))
            set(FEA.edit.y,'string',num2str(mean(ylim)))
            set(FEA.edit.z,'string',num2str([zlim]+[r -r]))
        end
    else
        set(FEA.edit.r,'string','')
        set(FEA.edit.y,'string','')
        set(FEA.edit.z,'string','')
    end
case 'save'
    FEA=get(gcbf,'userdata');
    if isempty(FEA.Filename)
        errordlg('Please load any FEM file first')
        return
    end
    if nargin==1
        [Filename Path]=uiputfile([FEA.Path FEA.Filename '.fea']);
    else
        if strcmp(varargin{2},'LoadDir')
            Filename=[varargin{3} '.fea'];
            Path=varargin{4};
        else
            return
        end
    end
    if strcmp('0',num2str(Filename)) | strcmp('0',num2str(Path));
        return
    end
    h_msgbox=msgbox(['Please sit back and relax. ' char(13) 'It will take a while.']);
    if get(FEA.rad.no,'value')
    else
        r=str2num(get(FEA.edit.r,'string'));
        if isempty(r)| r<=0 | ~isreal(r) 
            errordlg('Radius has to be greater than zero');
            if ishandle(h_msgbox)
                delete(h_msgbox)
            end
            return
        end
        y=str2num(get(FEA.edit.y,'string'));
        if isempty(y) | ~isreal(y) 
            errordlg('Y has to be number');
            if ishandle(h_msgbox)
                delete(h_msgbox)
            end
            return
        end
        z=str2num(get(FEA.edit.z,'string'));
        if isempty(z) | ~isreal(z) 
            errordlg('Z has to be number');
            if ishandle(h_msgbox)
                delete(h_msgbox)
            end
            return
        end
        length_y=length(y);
        length_z=length(z);
        if get(FEA.rad.round,'value')
            if length_y>1 | length_z>1
                errordlg('Bad y z value')
                if ishandle(h_msgbox)
                    delete(h_msgbox)
                end
                return
            end
            temp_bad=find(((FEA.yz(:,1)-y).^2+(FEA.yz(:,2)-z).^2) > r^2);
        elseif get(FEA.rad.box,'value')
            if length_y==2 & length_z==2
                temp1=find((FEA.yz(:,1))<y(1));
                temp2=find((FEA.yz(:,2))<z(1));
                temp3=find((FEA.yz(:,1))>y(2));
                temp4=find((FEA.yz(:,2))>z(2));
                temp_bad=[temp1;temp2;temp3;temp4];
            elseif length_y==2
                temp1=find((FEA.yz(:,1))<=y(1));
                temp2=find((FEA.yz(:,1))>=y(2));
                temp3=find(((FEA.yz(temp1,1)-y(1)).^2+(FEA.yz(temp1,2)-z).^2)>r^2);
                temp4=find(((FEA.yz(temp2,1)-y(2)).^2+(FEA.yz(temp2,2)-z).^2)>r^2);
                temp_bad=[temp1(temp3); temp2(temp4)];
            elseif length_z==2
                temp1=find((FEA.yz(:,1))<=z(1));
                temp2=find((FEA.yz(:,1))>=z(2));
                temp3=find(((FEA.yz(temp1,1)-y)).^2+((FEA.yz(temp1,2)-z(1)).^2)>r^2);
                temp4=find(((FEA.yz(temp2,1)-y)).^2+((FEA.yz(temp2,2)-z(1)).^2)>r^2);
                temp_bad=[temp1(temp3); temp2(temp4)];
            end
        end
        temp=[];
        for i=1:length(temp_bad)
            temp=[temp;find(FEA.FEM(:,2)==FEA.yz(temp_bad(i),1) & FEA.FEM(:,3)==FEA.yz(temp_bad(i),2))];
        end
        FEA.yz(temp_bad,:)=[];
        FEA.FEM(temp,:)=[]; 
    end
    tic
    if 1
        dlmwrite([Path Filename],FEA.FEM,'\t')
    else
        fid=fopen([Path Filename],'wb');
        fprintf(fid,'%8.2f %8.2f %8.2f %8.4f\n',FEA.FEM')
        fclose(fid)
    end
    toc
    %---------------------------------------
    %Do not save the change
    %---------------------------------------
    figure
    axes
    for i=1:length(FEA.yz)
        patch(FEA.yz(i,1),FEA.yz(i,2),'b',...
            'marker','o',...
         	'LineWidth',1.25,...   
			'MarkerEdgeColor','b',...
			'MarkerFaceColor','none',...
   			'MarkerSize',8);
	 end
     ylim=[min(FEA.yz(:,1))-10 max(FEA.yz(:,1))+10];
     zlim=[min(FEA.yz(:,2))-10 max(FEA.yz(:,2))+10];
     axis([ylim zlim])
     if ishandle(h_msgbox)
        delete(h_msgbox)
     end
end

Contact us at files@mathworks.com