clear all;
close all;
dat.meta.cellNum = 2;
dirID = ['C:\Documents and Settings\Annihilatrix II\Desktop\mingfaifong\HEKA data\2013-04-03_dojo7_26DIV_Patch_OU-ArchT\from HEKA\cell0' ...
    num2str(dat.meta.cellNum) '\'];


y = dir(dirID); z = [y.isdir];
fidlist = {y(~z).name};


for j = 1:length(fidlist)
    clear Trace* x*
    
    fid = fidlist{j};
    load([dirID fid]);
   
    x = who('Trace_*');

    
    protocolList = {'UpFreqChirp'; ...
        'OU-Rep-5ms'; ...
        'OU-Rep-50ms'; ...
        'OU-Rand-5ms'; ...
        'OU-Rand-50ms'; ...
        'PBRS-1ms'; ...
        'PBRS-10ms'; ...
        'PowSteps-1s'; ...
        'PowSteps-10s'
        %'DownFreqChirp'; ...
        %'PowSteps'; ...
        %'OU-Rand'; ...
        %'OU-Rep2'; ...
        %'OU-Rep3'; ...
        %'OU-Rep'; ...
        %'CurrPulse'; ...
        %'HypCurrPulse'; ...
        %'LongCurr' ...
        };
    
    for k = 1:length(protocolList)
        if findstr(fid,protocolList{k})
            dat.meta.protocolType = protocolList{k};
            if k>=10
                tmp_tracesPerSweep = 2;
            else
                tmp_tracesPerSweep = 4;
            end
            break;
        elseif k == length(protocolList)
            dat.meta.protocolType = 'unknown';
            disp('protocol not found');
            keyboard;
        else
            continue;
        end
    end
    dat.meta.sweepTotal = length(x)/tmp_tracesPerSweep;
    
    if dat.meta.sweepTotal==10
        x = [x(5:end); x(1:4)];
    elseif dat.meta.sweepTotal>10
        i_one = 45:48;
        i_two = 89:92;
        i_three = 133:136;
        i_four = 177:180;
        i_five = 221:224;
        i_six = 265:268;
        i_seven = 309:312;
        i_eight = 353:356;
        i_nine = 397:400;
        i_tens = 5:44;
        i_twenties = 49:88;
        i_thirties = 93:132;
        i_forties = 137:176;
        i_fifties = 181:220;
        i_sixties = 225:264;
        i_seventies = 269:308;
        i_eighties = 313:352;
        i_nineties = 357:396;
        i_onehundred = 1:4;

        if dat.meta.sweepTotal==100
            x = [x(i_one); x(i_two); x(i_three); x(i_four); x(i_five); x(i_six); x(i_seven); x(i_eight); x(i_nine); ...
                x(i_tens); x(i_twenties); x(i_thirties); x(i_forties); x(i_fifties); x(i_sixties); x(i_seventies); x(i_eighties); x(i_nineties); ...
                x(i_onehundred)];
        else
            dat.meta.sweepTotal
            keyboard;
            % for 78: x = [x(41:44); x(85:88); x(129:132); x(173:176);
            % x(217:220); x(261:264); x(301:312); x(1:40); x(45:84); x(89:128); x(133:172); x(177:216); x(221:260); x(265:300)];
        end
%         x = [x(45:48); x(89:92); x(133:136); x(177:180); x(221:224); x(265:268); x(309:312); x(354:356); x(397:400); ...
%             x(5:44); x(49:88); x(93:132); x(137:176); x(181:220); x(225:264); x(269:308); x(313:353); x(357:396); ...
%             x(1:4)];
    elseif dat.meta.sweepTotal>=70 && dat.meta.sweepTotal<80
        x = [x(45:48); x(89:92); x(133:136); x(177:180); x(221:224); x(265:268); x(309:312); x(354:356); x(397:400); ...
            x(5:44); x(49:88); x(93:132); x(137:176); x(181:220); x(225:264); x(269:308); x(313:353); x(357:396); ...
            x(1:4)];
    end
    
    if findstr(fid,'_CC')
        dat.meta.controlType = 'Current Clamp';
        ctrl_tmp = 'CC';
    else
        dat.meta.controlType = 'Voltage Clamp';
        ctrl_tmp = 'VC';
    end
    
    uloc = findstr(fid,'_');
    dat.meta.date = fid(1:uloc(1)-1);
    dat.meta.culture = fid(uloc(1)+1:uloc(2)-1);
    dat.meta.HEKA.fileName = fid(1:uloc(3)-1);
    dat.meta.HEKA.groupName = fid(uloc(3)+1:uloc(4)-1);
    dat.meta.HEKA.seriesName = fid(uloc(4)+1:end-4);
    dloc = findstr(dat.meta.HEKA.seriesName,'-');
    if length(dat.meta.HEKA.seriesName)-dloc(end)<=1
        seriesNum_tmp = ['0' dat.meta.HEKA.seriesName(dloc(end)+1)];
    else
        seriesNum_tmp = dat.meta.HEKA.seriesName(dloc(end)+1:dloc(end)+2);
    if seriesNum_tmp(2) == '_' || seriesNum_tmp(2) == '.'
        seriesNum_tmp = ['0' seriesNum_tmp(1)];
    end
    end
    dat.meta.HEKA.seriesNum = str2double(seriesNum_tmp);
        
    for i = 1:dat.meta.sweepTotal
        prefix_tmp = ['dojo7_cell0' num2str(dat.meta.cellNum) '_' dat.meta.protocolType '_' ctrl_tmp '_' seriesNum_tmp];
        if length(num2str(i))==1
            dat.meta.name = [prefix_tmp '_00' num2str(i)];
        elseif length(num2str(i))==2
            dat.meta.name = [prefix_tmp '_0' num2str(i)];
        elseif length(num2str(i))==3
            dat.meta.name = [prefix_tmp '_' num2str(i)];
        end

        x_Imem = eval(char(x(1+(tmp_tracesPerSweep*(i-1)))));
        x_Vmem = eval(char(x(2+(tmp_tracesPerSweep*(i-1)))));
        dat.raw.Imem = x_Imem(:,2);
        dat.raw.Vmem = x_Vmem(:,2);
        dat.meta.units.Imem = 'A';
        dat.meta.units.Vmem = 'V';

        if tmp_tracesPerSweep==4
                x_Iled = eval(char(x(3+(tmp_tracesPerSweep*(i-1)))));
                x_Vpd = eval(char(x(4+(tmp_tracesPerSweep*(i-1)))));
                dat.raw.Iled = x_Iled(:,2);
                dat.raw.Vpd = x_Vpd(:,2);
                dat.meta.units.Vled = 'V';
                dat.meta.units.Iled = 'A';
                dat.meta.units.Ipd = 'A';
        end
        
        dat.meta.fs_hz = 1/mean(diff(x_Imem(:,1)));
        %dat.meta.sweepNum = i;
        tmp_tracename = char(x(1+(tmp_tracesPerSweep*(i-1))));
        uloc_tracename = findstr(tmp_tracename,'_'); 
        dat.meta.sweepNum = str2double(tmp_tracename(uloc_tracename(3)+1:uloc_tracename(4)-1));
        dat.meta.sweepLengthSec = max(x_Imem(:,1))-min(x_Imem(:,1));
        dat.meta.wasSweep = 1;
        
%         save([dirID dat.meta.name '.mat'],'dat');
%        save([dat.meta.name '.mat'],'dat');
        save(['C:\Documents and Settings\Annihilatrix II\Desktop\mingfaifong\HEKA data\2013-04-03_dojo7_26DIV_Patch_OU-ArchT\matfiles\' ...
            dat.meta.name '.mat'],'dat');

        %         dat.raw.Imem = x_Imem;
%         dat.raw.Vmem = x_Vmem;
%         dat.raw.Vctl = [];
%         dat.raw.Iled = x_Iled;
%         dat.raw.Vpd = x_Vpd;
%        dat.meta.name = [dat.meta.name 't'];


    end
    
end