%
disp(' ');
disp(' data_input.m  ver 1.0  November 24, 2010 ');
disp(' by Tom Irvine   Email: tomirvine@aol.com');
disp(' ');
disp(' This script removes header lines from ');
disp(' an external ASCII text file. ');
disp(' It also divides a multi-channel file');
disp(' into separate channels. ');
disp(' ');
%
clear mdata;
j=1;
%
[filename, pathname] = uigetfile('*.*');
filename = fullfile(pathname, filename); 
fid = fopen(filename,'r');
%
disp(' ');
disp(' Is the first column a time column? ');
disp(' 1=yes  2=no ');
fc=input(' ');
%
if(fc==1)
    disp(' ');
    disp(' Select time format ');
    disp(' 1= sec ');
    disp(' 2= hr:min:sec ');
    itf=input(' ');
end
%
if(fc==2)
    tstart=input(' Enter starting time(sec) ');
        sr=input(' Enter sample rate (samples/sec)');
        dt=1/sr;
end
%
for(i=1:10000000)
%
            clear THF;
            THF = fgets(fid);
            if(THF==-1)
                if(max(size(THF))==1)
                    break;
                end
            end
%
            clear NC;
            NC=strrep(THF, ',', ' ');
            THF=NC;
            clear NC;
            NC=strrep(THF, ':', ' ');
            THF=NC;
%
            iflag=0;
            k = findstr(THF,'a');
            if(k>=1)
                iflag=1;
            end
            k = findstr(THF,'b');
            if(k>=1)
                iflag=1;
            end
            k = findstr(THF,'c');
            if(k>=1)
                iflag=1;
            end  
            k = findstr(THF,'d');
            if(k>=1)
                iflag=1;
            end     
            k = findstr(THF,'e');
            g = findstr(THF,'e+');
            h = findstr(THF,'e-');
            if(g>=1)
            else
                if(h>=1)
                else
                    if(k>=1)
                        iflag=1;
                    end
                end
            end
            k = findstr(THF,'f');
            if(k>=1)
                iflag=1;
            end
            k = findstr(THF,'g');
            if(k>=1)
                iflag=1;
            end
            k = findstr(THF,'h');
            if(k>=1)
                iflag=1;
            end  
            k = findstr(THF,'i');
            if(k>=1)
                iflag=1;
            end     
            k = findstr(THF,'j');
            if(k>=1)
                iflag=1;
            end       
            k = findstr(THF,'k');
            if(k>=1)
                iflag=1;
            end
            k = findstr(THF,'l');
            if(k>=1)
                iflag=1;
            end
            k = findstr(THF,'m');
            if(k>=1)
                iflag=1;
            end  
            k = findstr(THF,'n');
            if(k>=1)
                iflag=1;
            end     
            k = findstr(THF,'o');
            if(k>=1)
                iflag=1;
            end       
            k = findstr(THF,'p');
            if(k>=1)
                iflag=1;
            end
            k = findstr(THF,'q');
            if(k>=1)
                iflag=1;
            end
            k = findstr(THF,'r');
            if(k>=1)
                iflag=1;
            end  
            k = findstr(THF,'s');
            if(k>=1)
                iflag=1;
            end     
            k = findstr(THF,'t');
            if(k>=1)
                iflag=1;
            end   
            k = findstr(THF,'u');
            if(k>=1)
                iflag=1;
            end
            k = findstr(THF,'v');
            if(k>=1)
                iflag=1;
            end  
            k = findstr(THF,'w');
            if(k>=1)
                iflag=1;
            end     
            k = findstr(THF,'x');
            if(k>=1)
                iflag=1;
            end     
            k = findstr(THF,'y');
            if(k>=1)
                iflag=1;
            end     
            k = findstr(THF,'z');
            if(k>=1)
                iflag=1;
            end    
            k = findstr(THF,'A');
            if(k>=1)
                iflag=1;
            end
            k = findstr(THF,'B');
            if(k>=1)
                iflag=1;
            end
            k = findstr(THF,'C');
            if(k>=1)
                iflag=1;
            end  
            k = findstr(THF,'D');
            if(k>=1)
                iflag=1;
            end     
            k = findstr(THF,'E');
            g = findstr(THF,'E+');
            h = findstr(THF,'E-');      
            k = findstr(THF,'F');
            if(g>=1)
            else
                if(h>=1)
                else
                    if(k>=1)
                        iflag=1;
                    end
                end
            end          
            k = findstr(THF,'G');
            if(k>=1)
                iflag=1;
            end
            k = findstr(THF,'H');
            if(k>=1)
                iflag=1;
            end  
            k = findstr(THF,'I');
            if(k>=1)
                iflag=1;
            end     
            k = findstr(THF,'J');
            if(k>=1)
                iflag=1;
            end       
            k = findstr(THF,'K');
            if(k>=1)
                iflag=1;
            end
            k = findstr(THF,'L');
            if(k>=1)
                iflag=1;
            end
            k = findstr(THF,'M');
            if(k>=1)
                iflag=1;
            end  
            k = findstr(THF,'N');
            if(k>=1)
                iflag=1;
            end     
            k = findstr(THF,'O');
            if(k>=1)
                iflag=1;
            end       
            k = findstr(THF,'P');
            if(k>=1)
                iflag=1;
            end
            k = findstr(THF,'Q');
            if(k>=1)
                iflag=1;
            end
            k = findstr(THF,'R');
            if(k>=1)
                iflag=1;
            end  
            k = findstr(THF,'S');
            if(k>=1)
                iflag=1;
            end     
            k = findstr(THF,'T');
            if(k>=1)
                iflag=1;
            end   
            k = findstr(THF,'U');
            if(k>=1)
                iflag=1;
            end
            k = findstr(THF,'V');
            if(k>=1)
                iflag=1;
            end  
            k = findstr(THF,'W');
            if(k>=1)
                iflag=1;
            end     
            k = findstr(THF,'X');
            if(k>=1)
                iflag=1;
            end     
            k = findstr(THF,'Y');
            if(k>=1)
                iflag=1;
            end     
            k = findstr(THF,'Z');
            if(k>=1)
                iflag=1;
            end    
            k = findstr(THF,'/');
            if(k>=1)
                iflag=1;
            end     
            k = findstr(THF,'\');
            if(k>=1)
                iflag=1;
            end     
            k = findstr(THF,'*');
            if(k>=1)
                iflag=1;
            end     
            k = findstr(THF,'%');
            if(k>=1)
                iflag=1;
            end    
            k = findstr(THF,'=');
            if(k>=1)
                iflag=1;
            end 
            k = findstr(THF,'#');
            if(k>=1)
                iflag=1;
            end  
            k = findstr(THF,'=');
            if(k>=1)
                iflag=1;
            end   
%  
            if(iflag==0)
%
                k = findstr(THF,'0');
                if(k>=1)
                    iflag=2;
                end     
                k = findstr(THF,'1');
                if(k>=1)
                    iflag=2;
                end     
                k = findstr(THF,'2');
                if(k>=1)
                    iflag=2;
                end     
                k = findstr(THF,'3');
                if(k>=1)
                    iflag=2;
                end  
                k = findstr(THF,'4');
                if(k>=1)
                    iflag=2;
                end         
                k = findstr(THF,'5');
                if(k>=1)
                    iflag=2;
                end 
                k = findstr(THF,'6');
                if(k>=1)
                    iflag=2;
                end  
                k = findstr(THF,'7');
                if(k>=1)
                    iflag=2;
                end 
                k = findstr(THF,'8');
                if(k>=1)
                    iflag=2;
                end  
                k = findstr(THF,'9');
                if(k>=1)
                    iflag=2;
                end          
            end
%
            if(iflag==2)
                clear aaa;
                aaa = sscanf(THF,'%g');
                aaa=aaa';
                mdata(j,:)=aaa;
                j=j+1;
            end
%            
end
disp(' ');
disp(' Data stored in Matlab matrix:  mdata ');
disp(' ');
disp(' View mdata ?');
disp(' 1=yes  2=no ');
iv=input(' ');
if(iv==1)
    mdata
end
sn=size(mdata);
sz=sn(2);
%
clear length;
clear fname;
clear tc;
%
if(fc==1 && itf==1)
%
    for(i=1:200)
        if(sz>=i+1)
            str=sprintf('mdata%d',i);
            varname = genvarname(str);
            eval([varname ' = [mdata(:,1),mdata(:,i+1)];']);
            fname{i}=sprintf(' %s ',varname);
        else
            break;
        end
    end
%    
end  
%
if(fc==1 && itf==2)
%
    clear tc;
    tc=3600*mdata(:,1)+60*mdata(:,2)+mdata(:,3);
%
    disp(' Set initial time to zero ');
    disp(' 1=yes  2=no ');
    itime=input(' ');
    if(itime==1)
        tc=tc-tc(1);
    end
%
    for(i=1:200)
        if(sz>=i+3)
            str=sprintf('mdata%d',i);
            varname = genvarname(str);
            eval([varname ' = [tc,mdata(:,i+3)];']);
            fname{i}=sprintf(' %s ',varname);
        else
            break;
        end
    end
%    
end  
%
if(fc==2)
%
    for(i=1:sn(1))
        tc(i)=tstart+(i-1)*dt;
    end
    tc=tc';
%
    for(i=1:200)
        if(sz>=i)
            str=sprintf('mdata%d',i);
            varname = genvarname(str);
            eval([varname ' = [tc,mdata(:,i)];']);
            fname{i}=sprintf(' %s ',varname);
        else
            break;
        end
    end    
end
%
disp(' ');
disp(' Output filenames ');
disp(' ');
%
if(fc==1 && itf==1)
%
    for(i=1:(sz-1))
        out1=sprintf(' %s ',fname{i});
        disp(out1);    
    end
end
%
if(fc==1 && itf==2)
%
    for(i=1:(sz-3))
        out1=sprintf(' %s ',fname{i});
        disp(out1);    
    end
end
%
if(fc==2)
%
    for(i=1:sz)
        out1=sprintf(' %s ',fname{i});
        disp(out1);    
    end    
end