%function [P T Test T2] = readEpi( path, type )
%
%Example: [P T Test T2] = readEpi({'../data/pac2.xls','../data/pac10.xls'},'ii');
%
%Read n xls files, this read depend on type:
%
%   type = 'i' > Merge all the files in a single matrix, in each 3 one is
%   for test (P = 70%, Test=30%).
%
%   type = 'ii' > Merge all the files in a single matrix (P), excepting the last
%   that is the Test matrix.
%
%   type = 'iii' > Merge all the files in a single matrix (P), the last one
%   is the Test matrix
%
% Copyright 2007-2008 Rui P. Costa (http://student.dei.uc.pt/~racosta)
%
function [P T Test T2] = readEpi( path, type )    

    
    switch(type)
        case 'i' 
                
                %0. Read and normalize
                m=readNorm(path);
                 
                %1. Create Test matrix (Test)
                Test=m(1:3:end,:);
                m(1:3:end,:)=[];      

                %2. Create the target matrix (T)
                T=zeros(4,length(m));
                for i = 1:length(m),
                    n = m(i,1);
                    T(n,i)=1;
                end

                %3. Create the target for Test (T2)
                T2=Test(:,1); 

                %4. Create and transpose P
                P=m(:,2:end);
                P=P';

                %5. Clean and transpose Test
                Test=Test(:,2:end);
                Test=Test';
            

        case 'ii'                     
                    %0. Read and normalize
                    m=readNorm(path(1:(end-1)));
                    Test=readNorm(path(end));

                    %1. Create the target matrix (T)
                    T=zeros(4,length(m));
                    for i = 1:length(m),
                        n = m(i,1);
                        T(n,i)=1;
                    end

                    %2. Create the target for Test (T2)
                    T2=Test(:,1); 

                    %3. Create and transpose P
                    P=m(:,2:end);
                    P=P';

                    %4. Clean and transpose Test
                    Test=Test(:,2:end);
                    Test=Test';
            
        case 'iii'                    
                    %0. Read and normalize
                    m=readNorm(path);
                    Test=readNorm(path(end));

                    %1. Create the target matrix (T)
                    T=zeros(4,length(m));
                    for i = 1:length(m),
                        n = m(i,1);
                        T(n,i)=1;
                    end

                    %2. Create the target for Test (T2)
                    T2=Test(:,1); 

                    %3. Create and transpose P
                    P=m(:,2:end);
                    P=P';

                    %4. Clean and transpose Test
                    Test=Test(:,2:end);
                    Test=Test';  
    end   
    
    function m=readNorm(path)
        m=[];
        %1. Read from the Excel file
        for i=1:length(path),
            mAux=xlsread(char(path(i)));
            
            %1.1 Normalize matrix by column
            aux=mAux(:,1);
            aux2=mAux(:,2:end);
            for j=1:length(aux2(1,:))
                aux2(:,j)=aux2(:,j)/max(max(aux2(:,j)));
            end    
            mAux=horzcat(aux,aux2);   
            
            m=vertcat(m,mAux);
        end 
        