function [der, t] = readMS(filename,deriv)

%   [der t] = readMS(filename, deriv)   
%
%   This function reads an ECG record file from the metabolic syndrome study database. 
%   According to the input value it decides which derivation the user wants
%   and returns the desired derivation and a time axis to plot the return
%   values. The returned signals have a 1kHz sampling frequency.
%
%   Inputs:
%
%   'filename' is a string containing the full path to the
%   registry that the user wants to read; the .csv extension MUST be included
%   in the file path. 
%
%   'deriv' is a string containing the name of the derivation to be read 
%   ('DI', 'DII', 'DIII', 'aVR', 'aVF', 'aVL', 'V1', 'V2', 'V2', 'V3',
%   'V4', 'V5' or 'V6') or 'all' if the user wishes to read all derivations.
%
%   Outputs:
%
%   'der' is an array of double values. If the user chooses a
%   single derivation then 'der' will be a single column array; if the user
%   chooses all derivations then 'der' will be a 12 column array containing
%   one derivation in each column as follows:
%
%   DI DII DIII aVR aVL aVF V1 V2 V3 V4 V5 V6
%
%   't' is a time axis that is coherent with the read derivations.
%
%   This routine was developed by the Simon Bolivar University's Applied
%   Biophysics and Bioengineering Group (GBBA-USB) for free and public use. 
%   It is protected under the terms of the Creative Commons Attribution-Non
%   Commercial 4.0 International License, no profit may come from any 
%   application that uses this function and proper credit must be given to 
%   the GBBA-USB. For further information consult 
%   http://creativecommons.org/licenses/by-nc/4.0/
 

A = dlmread(filename,';');
t = 0 : 1/1000 : (length(A)-1)/1000;

switch deriv
    case 'all'
        der = A;
        for i=1:4:12
            figure,
           for j=0:3
               subplot(4,1,j+1), plot(t,A(:,i+j))
               xlim([450 460]), xlabel('Time [seg]'), ylabel('Amplitude');
               switch j+i
                   case 1,
                       title('DI part')
                   case 2,
                       title('DII part')
                   case 3
                       title('DIII part')
                   case 4
                       title('aVR part')
                   case 5
                       title('aVL part')
                   case 6
                       title('aVF part')
                   case 7
                       title('V1 part')
                   case 8
                       title('V2 part')
                   case 9
                       title('V3 part')
                   case 10
                       title('V4 part')
                   case 11
                       title('V5 part')
                   case 12
                       title('V6 part')
               end
        end
        
        end
    case 'DI'
        der = A(:,1);
        figure,
        plot(t,der);
        xlim([450 460]), xlabel('Time [seg]'), ylabel('Amplitude');
        title('DI part');
    case 'DII'
        der = A(:,2);
        figure,
        plot(t,der);
        xlim([450 460]), xlabel('Time [seg]'), ylabel('Amplitude');
        title('DII part');
    case 'DIII'
        der = A(:,3);
        figure,
        plot(t,der);
        xlim([450 460]), xlabel('Time [seg]'), ylabel('Amplitude');
        title('DIII part');
    case 'aVR'
        der = A(:,4);
        figure,
        plot(t,der);
        xlim([450 460]), xlabel('Time [seg]'), ylabel('Amplitude');
        title('aVR part');
    case 'aVL'
        der = A(:,5);
        figure,
        plot(t,der);
        xlim([450 460]), xlabel('Time [seg]'), ylabel('Amplitude');
        title('aVL part');
    case 'aVF'
        der = A(:,6);
        figure,
        plot(t,der);
        xlim([450 460]), xlabel('Time [seg]'), ylabel('Amplitude');
        title('aVF part');
    case 'V1'
        der = A(:,7);
        figure,
        plot(t,der);
        xlim([450 460]), xlabel('Time [seg]'), ylabel('Amplitude');
        title('V1 part');
    case 'V2'
        der = A(:,8);
        figure,
        plot(t,der);
        xlim([450 460]), xlabel('Time [seg]'), ylabel('Amplitude');
        title('V2 part');
    case 'V3'
        der = A(:,9);
        figure,
        plot(t,der);
        xlim([450 460]), xlabel('Time [seg]'), ylabel('Amplitude');
        title('V3 part');
    case 'V4'
        der = A(:,10);
        figure,
        plot(t,der);
        xlim([450 460]), xlabel('Time [seg]'), ylabel('Amplitude');
        title('V4 part');
    case 'V5'
        der = A(:,11);
        figure,
        plot(t,der);
        xlim([450 460]), xlabel('Time [seg]'), ylabel('Amplitude');
        title('V5 part');
    case 'V6'
        der = A(:,12);
        figure,
        plot(t,der);
        xlim([450 460]), xlabel('Time [seg]'), ylabel('Amplitude');
        title('V6 part');
        
end
end
