Browse Source

파일 업로드 'Matlab_function'

SeungWooJin 3 years ago
parent
commit
c1431fddcf

+ 48 - 0
Matlab_function/GetLinearRegression.m

@@ -0,0 +1,48 @@
+function [Regression, f] = GetLinearRegression(x,y,varargin)
+
+if isempty(varargin)
+    flag=0;
+    f=0;
+else
+    flag=1;
+    g = varargin{1};
+end
+
+j=1;
+for i=1:length(x)
+    if ~isnan(x(i)) && ~isnan(y(i))
+        x1(j)=x(i); y1(j)=y(i);
+        j=j+1;
+    end
+end
+if exist('x1')
+    [r, p] = corrcoef(x1,y1);
+    Regression.R=r(1,2);
+    Regression.Rsquared=r(1,2)^2;
+    Regression.Pvalue=p(1,2);
+
+    if flag
+        [a] = polyfit(x1,y1,1);
+        f=polyval(a,x1);
+        [r2, rmse] = rsquare(y1,f);
+        Regression.a0=a(2); Regression.a1=a(1); % f(x) = a0 + a1*x
+        Regression.x2=[g.XLim(1) g.XLim(2)];
+        Regression.y2=Regression.a0+Regression.a1*Regression.x2;
+    end
+else
+    Regression.R=NaN;
+    Regression.Rsquared=NaN;
+    Regression.Pvalue=NaN;
+end
+%     p3=plot(x2,y2,'k-');
+%     p3.LineWidth=2;
+%     deltaY=(g.YLim(2)-g.YLim(1))/10;
+%     deltaX=(g.XLim(2)-g.XLim(1))/10;
+    
+%     t1=text((g.XLim(1)+3*deltaX), (g.YLim(2)-4*deltaY),['p-value : ' num2str(round(Pvalue,3))]);
+%     t1.FontSize=fontsize.axis; t1.FontWeight='bold';
+%     t2=text((GCA.XLim(1)+3*deltaX), (GCA.YLim(2)-3*deltaY),['r-squared : ' num2str(round(Rsquared,3))]);
+%     t2.FontSize=fontsize.axis; t2.FontWeight='bold';
+%     t3=text((g.XLim(1)+3*deltaX), (g.YLim(2)-2*deltaY),['r : ' num2str(round(r(1,2),3))]);
+%     t3.FontSize=15; t3.FontWeight='bold';    
+%     

+ 21 - 0
Matlab_function/GetNaNToNull.m

@@ -0,0 +1,21 @@
+function [Matrix, index] = GetNaNToNull(Matrix)
+% [col, row]=size(Matrix);
+index=find(isnan(Matrix));
+Matrix(isnan(Matrix))=[];
+% if col==1
+%     
+%     for i=1:row
+%         if isnan(Matrix(i))
+%             Matrix(i)=[];
+%         end
+%     end
+%     NanMatrix=Matrix;
+% elseif row == 1
+%     for i=1:col
+%         if isnan(Matrix(i))
+%             Matrix(i)=[];
+%         end
+%     end
+%     NanMatrix=Matrix;
+% end
+% end

+ 20 - 0
Matlab_function/GetPlaceFieldSize.m

@@ -0,0 +1,20 @@
+function [FieldSize] = GetPlaceFieldSize(SkaggsrateMat)
+%% Open field
+    Contour.FieldSize_Thre=max(max(SkaggsrateMat)) * 0.2;
+    Contour.FieldMap=SkaggsrateMat > Contour.FieldSize_Thre;
+
+    Contour.ContinuousMap=bwconncomp(Contour.FieldMap);
+    Contour.ContinuousLabled = labelmatrix(Contour.ContinuousMap);
+    Contour.IndividualFieldSize=0;
+    if ~isempty(Contour.ContinuousMap.PixelIdxList)
+        for k=1:length(Contour.ContinuousMap.PixelIdxList)
+            if length(Contour.ContinuousMap.PixelIdxList{1,k}) > 10 
+                Contour.IndividualFieldSize(k)=length(Contour.ContinuousMap.PixelIdxList{1,k});
+            end
+        end
+        FieldSize=sum(Contour.IndividualFieldSize);
+    else
+        FieldSize=0;
+    end
+
+    

+ 219 - 0
Matlab_function/GetSessionInfo.m

@@ -0,0 +1,219 @@
+function [Rat, Type, Day, TetrodeNumber, ClusterNumber, Session, Sessiontype, Index1] = GetSessionInfo(name) 
+a=strfind(name,'-');
+b=strfind(name,'Main');
+c=strfind(name,'.');
+if ~isempty(b)
+    Rat=name(a(1)+1:a(2)-1);
+    Type='Main';
+    Day=name(a(2)+5:a(3)-1);
+    TetrodeNumber=name(a(3)+3:a(4)-1);
+    ClusterNumber=name(a(4)+2:c(1)-1);
+else
+    Rat=name(a(1)+1:a(2)-1);
+    Type='Pilot';
+    Day=name(a(2)+6:a(3)-1);
+    TetrodeNumber=name(a(3)+3:a(4)-1);
+    ClusterNumber=name(a(4)+2:c(1)-1);
+end
+
+if str2num(Rat)==448 || str2num(Rat) == 459 || str2num(Rat) == 463
+        if str2num(Day)==3
+            Session='Alternation';
+            Sessiontype='SS';
+            Index1=1;
+        elseif str2num(Day)==4 
+            Session='Alternation';
+            Sessiontype='SSCH1';
+            Index1=2;
+        elseif str2num(Day)==5
+            Session='Alternation';
+            Sessiontype='SSCH2';
+            Index1=3;
+        elseif str2num(Day)==6 
+            Session='Alternation';
+            Sessiontype='FRCH1';
+            Index1=4;
+        elseif str2num(Day)==7
+            Session='Alternation';
+            Sessiontype='FRCH2';
+            Index1=5;
+        elseif str2num(Day)==8 
+            Session='Alternation';
+            Sessiontype='SSFR1';
+            Index1=6;
+        elseif str2num(Day)==9
+            Session='Alternation';
+            Sessiontype='SSFR2';
+            Index1=7;
+        elseif str2num(Day)==10 
+            Session='Tmaze';
+            Sessiontype='T-SSCH1';
+            Index1=14;     
+        elseif str2num(Day)==11 
+            Session='Tmaze';
+            Sessiontype='T-SSCH2';   
+            Index1=15;                 
+        elseif str2num(Day)==12
+            Session='Tmaze';
+            Sessiontype='T-Quantity1'; 
+            Index1=16; 
+        elseif str2num(Day)==13
+            Session='Tmaze';
+            Sessiontype='T-Quantity2'; 
+            Index1=17;   
+        elseif str2num(Day)==14
+            Session='Alternation';
+            Sessiontype='Quantity1';   
+            Index1=8;
+        elseif str2num(Day)==15
+            Session='Alternation';
+            Sessiontype='Quantity2'; 
+            Index1=9;    
+        end
+    elseif str2num(Rat)==473 || str2num(Rat) == 488
+        if str2num(Day)==1
+            Session='Alternation';
+            Sessiontype='SS';
+            Index1=1;
+        elseif str2num(Day)==2 
+            Session='Alternation';
+            Sessiontype='SSCH1';
+            Index1=2;
+        elseif str2num(Day)==3
+            Session='Alternation';
+            Sessiontype='SSCH2';
+            Index1=3;
+        elseif str2num(Day)==4 
+            Session='Alternation';
+            Sessiontype='FRCH1';
+            Index1=4;
+        elseif str2num(Day)==5
+            Session='Alternation';
+            Sessiontype='FRCH2';
+            Index1=5;
+        elseif str2num(Day)==6 
+            Session='Alternation';
+            Sessiontype='SSFR1';
+            Index1=6;
+        elseif str2num(Day)==7
+            Session='Alternation';
+            Sessiontype='SSFR2';
+            Index1=7;
+        elseif str2num(Day)==8 
+            Session='Tmaze';
+            Sessiontype='T-SSCH1';
+            Index1=14;     
+        elseif str2num(Day)==9 
+            Session='Tmaze';
+            Sessiontype='T-SSCH2';  
+            Index1=15;                  
+        elseif str2num(Day)==10
+            Session='Tmaze';
+            Sessiontype='T-Quantity1';  
+            Index1=16;
+        elseif str2num(Day)==11
+            Session='Tmaze';
+            Sessiontype='T-Quantity2';
+            Index1=17;    
+        elseif str2num(Day)==12
+            Session='Alternation';
+            Sessiontype='Quantity1';
+            Index1=8;   
+        elseif str2num(Day)==13
+            Session='Alternation';
+            Sessiontype='Quantity2';  
+            Index1=9; 
+        elseif str2num(Day)==14 
+            Session='Alternation';
+            Sessiontype='SSFRCH1';
+            Index1=10;
+        elseif str2num(Day)==15 
+            Session='Alternation';
+            Sessiontype='SSFRCH2';    
+            Index1=11;
+        elseif str2num(Day)==16
+            Session='Alternation';
+            Sessiontype='Object1';  
+            Index1=12;  
+        elseif str2num(Day)==17
+            Session='Alternation';
+            Sessiontype='Object2';   
+            Index1=13;    
+        end
+    elseif str2num(Rat)==509
+        if isequal(Type,'Pilot')
+            Session='Alternation';
+            Sessiontype='SS';
+            Index1=1;
+        else
+            if str2num(Day)==1
+                Session='Alternation';
+                Sessiontype='SS';
+                Index1=1;
+            elseif str2num(Day)==2 
+                Session='Alternation';
+                Sessiontype='SSCH1';
+                Index1=2;
+            elseif str2num(Day)==3
+                Session='Alternation';
+                Sessiontype='SSCH2';
+                Index1=3;
+            elseif str2num(Day)==4 
+                Session='Alternation';
+                Sessiontype='FRCH1';
+                Index1=4;
+            elseif str2num(Day)==5
+                Session='Alternation';
+                Sessiontype='FRCH2';
+                Index1=5;
+            elseif str2num(Day)==6 
+                Session='Alternation';
+                Sessiontype='SSFR1';
+                Index1=6;
+            elseif str2num(Day)==7
+                Session='Alternation';
+                Sessiontype='SSFR2';
+                Index1=7;
+            elseif str2num(Day)==8 
+                Session='Tmaze';
+                Sessiontype='T-SSCH1'; 
+                Index1=14;    
+            elseif str2num(Day)==9 
+                Session='Tmaze';
+                Sessiontype='T-SSCH2';
+                Index1=15;                    
+            elseif str2num(Day)==10
+                Session='Tmaze';
+                Sessiontype='T-Quantity1'; 
+                Index1=16; 
+            elseif str2num(Day)==11
+                Session='Tmaze';
+                Sessiontype='T-Quantity2'; 
+                Index1=17;   
+            elseif str2num(Day)==12
+                Session='Alternation';
+                Sessiontype='Quantity1';  
+                Index1=8; 
+            elseif str2num(Day)==13
+                Session='Alternation';
+                Sessiontype='Quantity2'; 
+                Index1=9;  
+            elseif str2num(Day)==14 
+                Session='Alternation';
+                Sessiontype='SSFRCH1';
+                Index1=10;
+            elseif str2num(Day)==15 
+                Session='Alternation';
+                Sessiontype='SSFRCH2';   
+                Index1=11; 
+            elseif str2num(Day)==16
+                Session='Alternation';
+                Sessiontype='Object1';  
+                Index1=12;  
+            elseif str2num(Day)==17
+                Session='Alternation';
+                Sessiontype='Object2';       
+                Index1=13;
+            end
+        end
+end

+ 12 - 0
Matlab_function/GetSwap.m

@@ -0,0 +1,12 @@
+function Data=GetSwap(data)
+if size(data,1)==1 && size(data,2) > 1
+    for i=1:length(data)
+        Data(i)=data(end-i+1);
+    end
+elseif size(data,1) > 1 && size(data,2) > 1
+    for i=1:size(data,1)
+        for j=1:size(data,2)
+            Data(i,j)=data(i,end-j+1);
+        end
+    end
+end

+ 11 - 0
Matlab_function/GetZeroToNaN.m

@@ -0,0 +1,11 @@
+function NanMatrix = GetZeroToNaN(Matrix)
+    [row, col]=size(Matrix);
+    for i=1:row
+        for j=1:col
+            if Matrix(i,j)==0
+                Matrix(i,j)=NaN;
+            end
+        end
+    end
+    NanMatrix=Matrix;
+end

+ 62 - 0
Matlab_function/Jin_Errorbar.m

@@ -0,0 +1,62 @@
+function [h, Mean, STD, STE] = Jin_Errorbar(x,y,varargin)
+
+
+% Example)
+% Width=0.3;
+% Color.color=53; Color.alpha=0.4;
+% h = JErrorbar(1,a.data1,Width,Color)
+
+if nargin == 2
+    Width = 0.35;
+    Color.color=4; Color.alpha=1;
+end
+
+if nargin == 3
+    Color.color=4; Color.alpha=1;
+    Width = varargin{1};
+end
+
+if nargin == 4
+    Width = varargin{1};
+    Color = varargin{2};
+end
+
+
+c=mapcolor(100,0);
+Mean=nanmean(y);
+STD=nanstd(y);
+STE=nanstd(y)/sqrt(length(y));
+
+x1=x-Width;
+x2=x+Width;
+y1=0;
+y2=Mean;
+% Box
+% h=fill([x1 x1 x2 x2],[y2 y1 y1 y2],[0.6 0.6 0.6]);
+h=fill([x1 x1 x2 x2],[y2 y1 y1 y2],c(Color.color,:));
+h.FaceAlpha=Color.alpha;
+h.Marker='none';
+h.EdgeColor='none';
+h.EdgeAlpha=0;
+% Line
+l1=line([x1 x1],[y2 y1],'LineWidth',0.6,'LineStyle','-'); l1.Color=[0 0 0];%l1.Color=c(Color.color,:);
+% l1=line([x1 x2],[y1 y1],'LineWidth',0.6,'LineStyle','-'); l1.Color=c(Color.color,:);
+l1=line([x1 x2],[y2 y2],'LineWidth',0.6,'LineStyle','-'); l1.Color=[0 0 0];%l1.Color=c(Color.color,:);
+l1=line([x2 x2],[y2 y1],'LineWidth',0.6,'LineStyle','-'); l1.Color=[0 0 0];%l1.Color=c(Color.color,:);
+
+% Std
+if Mean >= 0
+    l1=line([x x],[Mean (Mean+STE)],'LineWidth',0.6,'LineStyle','-'); l1.Color=[0 0 0]; %l1.Color=c(Color.color,:);
+    l1=line([(x-Width/3) (x+Width/3)],[(Mean+STE) (Mean+STE)],'LineWidth',0.6,'LineStyle','-'); l1.Color=[0 0 0]; %l1.Color=c(Color.color,:);
+elseif Mean < 0
+    l1=line([x x],[Mean (Mean-STE)],'LineWidth',0.6,'LineStyle','-'); l1.Color=[0 0 0]; %l1.Color=c(Color.color,:);
+    l1=line([(x-Width/3) (x+Width/3)],[(Mean-STE) (Mean-STE)],'LineWidth',0.6,'LineStyle','-'); l1.Color=[0 0 0]; %l1.Color=c(Color.color,:);
+end
+
+
+% for i=1:length(y)
+%     p1=plot(GetJitter(x,0.1),y(i),'.'); p1.Color=[0.8 0.2 0.2];
+% end
+% g=gca;
+% g.YColor=[0 0 0];
+% g.LineWidth=0.6;

+ 41 - 0
Matlab_function/chi2cont.m

@@ -0,0 +1,41 @@
+function [h,p,X2] = chi2cont(x,varargin)
+% chi2cont chi-square test of contingency table
+%   h = chi2cont(x) performs a chi-square test on the data in the
+%   m-by-n contingency table x. The null hypothesis is that there is no difference
+%   in the row variable distribution ('outcomes') between the columns 
+%   ('treatments'). The result of the test is returned in h. h=1 indicates
+%   a rejection of the null hypothesis at the 5% significance level.h=0
+%   indicates that the null hypothesis can not be rejected at the 5%
+%   significance level.
+%
+%   h = chi2cont(x,alpha) performs the test at the (100*alpha)%
+%   significance level. The default when unspecified is alpha=0.05;
+%   
+%   [h,p] = chi2cont(...) returns the p value of the test. The p value is 
+%   the probability, under the null hypothesis, of observing a value as 
+%   extreme or more extreme of the chi-square test statistic.
+%
+%   [h,p,X2] = chi2cont(...) returns the chi-square test statistic.
+%
+%   Reference http://www.psychstat.missouristate.edu/introbook/sbk28m.htm
+%
+%   Mark Snaterse, January 22 2014
+
+
+if isempty(varargin)
+    alpha = 0.05;
+elseif nargin>1
+    error('Too many input arguments')
+else
+    alpha = varargin{1};
+end
+
+% Compute expectation and chi-square statistic, and determine p value.
+e = sum(x,2)*sum(x)/sum(x(:));
+X2 = (x-e).^2./e;
+X2 = sum(X2(:));
+df = prod(size(x)-[1 1]);
+p = 1-chi2cdf(X2,df);
+
+h = double(p<=alpha);
+