% modified from PlotHekaDataVC by Jon Newman
% This script loads and plots Continuous VC HEKA data

% clear; pack;
% fid = '20100629_3-19_0to600s.mat';
% loc = 'J:\Documents and Settings\mkuykendal\Desktop\20100629_mingfaifong\10min chunks\';
% load(strcat([loc fid]));

% batch mode
loc = 'J:\Documents and Settings\mkuykendal\Desktop\20100629_mingfaifong\10min chunks\';
cd(loc);
dirdata = dir('*.mat');
datfiles = {dirdata.name};


fullscreen = get(0,'ScreenSize');

for j = 1:numel(datfiles)
    clearvars -except loc datfiles j fullscreen;
    %pack;
    fid = datfiles{j};
    load(fid);

savename = fid(1:length(fid)-4);

    N = 5; % number of plot rows
    numplots = N;
    seglen = floor(length(time)/N);
    low_y = min(ord)-50;
    high_y = max(ord)+50;
    
    for i = 2:N
        tmp = nanmean(ord(length(ord)-length(ord)/N*(N-i+1):length(ord)));
        if isnan(tmp)
            numplots = numplots-1;
        end
    end
    
    figure()
    for i = 1:numplots
        subplot(numplots,1,i)
        if i == N
            x = time((i-1)*seglen+1:i*seglen);
            y = ord((i-1)*seglen+1:i*seglen);
        else
            x = time((i-1)*seglen+1:i*seglen+1);
            y = ord((i-1)*seglen+1:i*seglen+1);        
        end
%         if min(x)
%         else
%             break;
%         end
        plot(x,y,'k');
        ylabel('$I_{mem} (pA)$','Interpreter','latex','FontSize',12);
            %ylim([-1100 100])
            ylim([low_y high_y]);
            %xlim([min(x) max(x)]);
            xlim([min(x) min(x)+600/N]);
            set(gca,'XTick',min(x):20:max(x))
            %set(gca,'XTick',(i-1)*600/N:20:i*600/N);
        if i == 1
            title(savename,'Interpreter','none','FontSize',12);
        end
        if i == numplots
            xlabel('$Time (s)$','Interpreter','latex','FontSize',12);
        end
    end
    saveas(gcf,['fig/' savename],'fig')
    set(gcf,'Position',[0 -50 fullscreen(3) fullscreen(4)])
    print('-djpeg',['jpg_12pt/' savename]);
    close;
    
end