%this is a program that plots place p value (minimum across all trials for
%each unit) vs. p value of stEODr
%this has been modified after finding out that three units had small p
%values but were not spatially specific
close all
clearvars
filenames=dir('*_Stage8_V6_output.mat');

filenames2=dir('*_Stage4_output.mat');
filenames3=dir('*_Stage2_output.mat');
for i=1:length(filenames)
    filenames(i).name
    load(filenames(i).name);
    load(filenames2(i).name);
    load(filenames3(i).name);
    [a,b]=size(uname);
    for k=1:a
        for j=1:b
            if isempty(uname{k,j})
                uname{k,j}='NaN';
            end
        end
    end
    for w=1:length(goodcells)
        cellname=strcat('U',goodcells(w));
        index=strmatch(cellname,uname);%find out which row/column that particular cell's data is located 
        [ii,jj]=ind2sub(size(uname),index);%i is trial number, j is the cell position in the matrix
         for c=1:length(ii)
                if length(nutimes{ii(c),jj(c)})>10
                    PP{w}(c)=P{jj(c)}(ii(c));
                else
                    PP{w}(c)=NaN;
                end
         end
         PP_Place{i}(w)=min(PP{w});
    end
    cellcnt(i)=sum(~isnan(PP_Place{i}))
    PP_EOD{i}=EOD_P(:,2)';
    if i==4
        index=1:length(goodcells);
        plot(1-PP_Place{i}([1,3]),1-EOD_P([1,3],2),'co')
        plot(1-PP_Place{i}(~ismember(index,[1,3])),1-EOD_P((~ismember(index,[1,3])),2),'ko')

    elseif i==5
        plot(1-PP_Place{i}(1),1-EOD_P(1,2),'co')
        plot(1-PP_Place{i}(2:end),1-EOD_P(2:end,2),'ko')
    else
        plot(1-PP_Place{i},1-EOD_P(:,2),'ko')
    end
    xlabel('P place')
    ylabel('P EOD')
    hold on
    clear PP
    %clear PP_Place
end
xlim([0.3,1])
ylim([0.3,1])
line([0,1],[0.95,0.95])
line([0.95,0.95],[0,1])
