close all; clear all;

dirnamelist = {'MVI_3U','MVI_4U','MVI_5U','MVI_6U','PCA_1D_4U','PCA_2D_4U','PCA_3D_4U','PCA_4D_4U','PCA_1D_5U','PCA_2D_5U','PCA_3D_5U','PCA_4D_5U'};
%dirnamelist = {'MVI_2U''};
fidlist = {'yoda363_chan55.mat','yoda363_chan56.mat','yoda363_chan58.mat','yoda363_chan59.mat','yoda363_chan60.mat'};
%fidlist = {'yoda363_chan55.mat','yoda363_chan56.mat'};

for k = 1:length(dirnamelist)
dirname = dirnamelist{k};
cd(['C:\Users\Jon\Desktop\mingledorf\yoda363\RapidSort\' dirname]);

figure('units','normalized','outerposition',[0 0 2 1])

for j = 1:length(fidlist)
fid = fidlist{j};
load(fid);
subplot(length(fidlist),7,(j-1)*7+1);

for i = 1:size(waveform,1)
    if unit(i)==1
        plot(waveform(i,:)','-','LineWidth',0.1,'Color',[255 0 0]/255);
    elseif unit(i)==2
        plot(waveform(i,:)','-','LineWidth',0.1,'Color',[255 204 51]/255);
    elseif unit(i)==3
        plot(waveform(i,:)','-','LineWidth',0.1,'Color',[51 153 0]/255);
    elseif unit(i)==4
        plot(waveform(i,:)','-','LineWidth',0.1,'Color',[0 153 255]/255);
    elseif unit(i)==5
        plot(waveform(i,:)','-','LineWidth',0.1,'Color',[153 0 255]/255);
    elseif unit(i)==6
        plot(waveform(i,:)','-','LineWidth',0.1,'Color',[255 102 255]/255);
    end
    hold on;
end
axis([1 50 -60e-6 60e-6]);
set(gca,'YTick',[-60e-6:20e-6:60e-6],'YTickLabel',[-60:20:60]);
ylabel('Voltage (uV)','FontSize',16);
set(gca,'XTick',[0:50/4:50],'XTickLabel',[0:0.5:2]);
title(fid(1:length(fid)-4),'Interpreter','none');

subplot(length(fidlist),7,(j-1)*7+2)
plot(waveform(unit==1,:)','-','LineWidth',0.1,'Color',[255 0 0]/255);
axis([0 50 -60e-6 60e-6])
set(gca,'YTick',[]);
axis([1 50 -60e-6 60e-6]);
set(gca,'XTick',[0:50/4:50],'XTickLabel',[0:0.5:2]);
title([num2str(length(unit(unit==1))) ' spikes'])

subplot(length(fidlist),7,(j-1)*7+3)
plot(waveform(unit==2,:)','-','LineWidth',0.1,'Color',[255 204 51]/255);
axis([0 50 -60e-6 60e-6])
set(gca,'YTick',[]);
axis([1 50 -60e-6 60e-6]);
set(gca,'XTick',[0:50/4:50],'XTickLabel',[0:0.5:2]);
title([num2str(length(unit(unit==2))) ' spikes'])

if max(unit) >= 3
subplot(length(fidlist),7,(j-1)*7+4)
plot(waveform(unit==3,:)','-','LineWidth',0.1,'Color',[51 153 0]/255);
axis([0 50 -60e-6 60e-6])
set(gca,'YTick',[]);
axis([1 50 -60e-6 60e-6]);
set(gca,'XTick',[0:50/4:50],'XTickLabel',[0:0.5:2]);
title([num2str(length(unit(unit==3))) ' spikes'])
end

if max(unit) >= 4
subplot(length(fidlist),7,(j-1)*7+5)
plot(waveform(unit==4,:)','-','LineWidth',0.1,'Color',[0 153 255]/255);
axis([0 50 -60e-6 60e-6])
set(gca,'YTick',[]);
axis([1 50 -60e-6 60e-6]);
set(gca,'XTick',[0:50/4:50],'XTickLabel',[0:0.5:2]);
title([num2str(length(unit(unit==4))) ' spikes'])
end

if max(unit) >= 5
subplot(length(fidlist),7,(j-1)*7+6)
plot(waveform(unit==5,:)','-','LineWidth',0.1,'Color',[153 0 255]/255);
axis([0 50 -60e-6 60e-6])
set(gca,'YTick',[]);
axis([1 50 -60e-6 60e-6]);
set(gca,'XTick',[0:50/4:50],'XTickLabel',[0:0.5:2]);
title([num2str(length(unit(unit==5))) ' spikes'])
end

if max(unit) >= 6
subplot(length(fidlist),7,(j-1)*7+7)
plot(waveform(unit==6,:)','-','LineWidth',0.1,'Color',[255 102 255]/255);
axis([0 50 -60e-6 60e-6])
set(gca,'YTick',[]);
axis([1 50 -60e-6 60e-6]);
set(gca,'XTick',[0:50/4:50],'XTickLabel',[0:0.5:2]);
title([num2str(length(unit(unit==6))) ' spikes'])
end

if j==length(fidlist)
    for m = 1:max(unit)
    subplot(length(fidlist),7,length(fidlist)*7-(7-m))
    xlabel('Time (ms)','FontSize',16);
    end
end

end

cd(['C:\Users\Jon\Desktop\mingledorf\yoda363\RapidSort\']);
    %print('-depsc',[dirname '_results.eps']);
    %print('-dpng',[dirname '_results.png']);
    clearvars -except dirnamelist dirname fidlist k
    screen2png([dirname '_results.png']);
    %saveas(gcf,[dirname '_results.fig'],'fig');
    close all;
    
    
end