function [ekg,atr]=rddata(jmeno,pathname)
%------ SPECIFY DATA ------------------------------------------------------

PATH= pathname; % path, where data are saved
HEADERFILE= [jmeno '.hea'];      % header-file in text format
ATRFILE= [jmeno '.atr'];         % attributes-file in binary format
DATAFILE=[jmeno '.dat'];         % data-file       
%------ LOAD HEADER DATA --------------------------------------------------
fprintf(1,'>>> Nacita se ', HEADERFILE);
signalh= fullfile(PATH, HEADERFILE);
fid1=fopen(signalh,'r');
z= fgetl(fid1);
A= sscanf(z, '%*s %d %d %d',[1,3]);
nosig= A(1);  % number of signals
sfreq=A(2);   % sample rate of data
pocetvz=A(3);
clear A;
for k=1:nosig
    z= fgetl(fid1);
    A= sscanf(z, '%*s %d %d %d %d %d',[1,5]);
    dformat(k)= A(1);           % format; here only 212 is allowed
    gain(k)= A(2);              % number of integers per mV
    bitres(k)= A(3);            % bitresolution
    zerovalue(k)= A(4);         % integer value of ECG zero point
    firstvalue(k)= A(5);        % first integer value of signal (to test for errors)
end;
fclose(fid1);
clear A;
%------ Zadni počtu vzorků -------------------------------------
% disp('Pro nacteni hodnot zadejte cislo nebo písmeno "a" pro vyber vsech hodnot')
% disp('')
% SAMPLES2READ=input(['>>>Prosím zadej kolik hodnot se má načíst ' , ' (Maximálně však ' num2str(pocetvz) ')  :  '],'s');       
SAMPLES2READ=pocetvz;
% if SAMPLES2READ=='a'
% else
%     SAMPLES2READ=str2double(SAMPLES2READ);
% end
% if SAMPLES2READ>0 && SAMPLES2READ<pocetvz 
%     disp('OK')
%     if SAMPLES2READ=='a'
%         SAMPLES2READ=pocetvz;
%     end
% else
    
%     disp('>>>Spatne zadaná data, opakujte zadání')
%     SAMPLES2READ=input(['>>>Prosím zadej kolik hodnot se má nacíst ' , ' (Maximálně vsak ' num2str(pocetvz) ')  :  ']);

    
% end
%% ------ LOAD BINARY DATA --------------------------------------------------

if dformat~= [212,212], error('>>>Aplikace nepracuje s jinými formáty jak formát 212.'); end;
signald= fullfile(PATH, DATAFILE);            % data in format 212
fid2=fopen(signald,'r');
A= fread(fid2, [3, SAMPLES2READ], 'uint8')';  % matrix with 3 rows, each 8 bits long, = 2*12bit
fclose(fid2);
M1H= bitand(A(:,2), 15);
PRL=bitshift(bitand(A(:,2),8),9);     % sign-bit
M(:,1)= bitshift(M1H,8)+ A(:,1)-PRL;
clear PRL M1H;
M2H= bitshift(A(:,2), -4);
PRR=bitshift(bitand(A(:,2),128),5);   % sign-bit
M(:,2)= bitshift(M2H,8)+ A(:,3)-PRR;
%save('M2.mat', 'M2')
clear PRR M2H;
%load('M.mat');
if M(1,:) ~= firstvalue, error('>>> Chyba dat.'); end;
switch nosig
case 2
    M( : , 1)= (M( : , 1)- zerovalue(1))/gain(1);
    M( : , 2)= (M( : , 2)- zerovalue(2))/gain(2);
    TIME=(0:(SAMPLES2READ-1))/sfreq;
case 1
    M( : , 1)= (M( : , 1)- zerovalue(1));
    M( : , 2)= (M( : , 2)- zerovalue(1));
    M=M';
    M(1)=[];
    sM=size(M);
    sM=sM(2)+1;
    M(sM)=0;
    M=M';
    M=M/gain(1);
    TIME=(0:2*(SAMPLES2READ)-1)/sfreq;
otherwise  % this case did not appear up to now!
    % here M has to be sorted!!!
    disp('>>>Více jak 2 signaly v zaznamu nejsou podporovany.');
end;
clear A M1H M2H;
fprintf(1,'>>> Data úspesne nactena.');

% %% ------ LOAD ATTRIBUTES DATA ----------------------------------------------
% --ATR--
atrd= fullfile(PATH, ATRFILE);      % attribute file with annotation data
fid3=fopen(atrd,'r');
A= fread(fid3, [2, inf], 'uint8')';
fclose(fid3);
ATRTIME=[];
ANNOT=[];
sa=size(A);
saa=sa(1);
i=1;
while i<=saa
    annoth=bitshift(A(i,2),-2);
    if annoth==59
        ANNOT=[ANNOT;bitshift(A(i+3,2),-2)];
        ATRTIME=[ATRTIME;A(i+2,1)+bitshift(A(i+2,2),8)+...
                bitshift(A(i+1,1),16)+bitshift(A(i+1,2),24)];
        i=i+3;
    elseif annoth==60
        % nothing to do!
    elseif annoth==61
        % nothing to do!
    elseif annoth==62
        % nothing to do!
    elseif annoth==63
        hilfe=bitshift(bitand(A(i,2),3),8)+A(i,1);
        hilfe=hilfe+mod(hilfe,2);
        i=i+hilfe/2;
    else
        ATRTIME=[ATRTIME;bitshift(bitand(A(i,2),3),8)+A(i,1)];
        ANNOT=[ANNOT;bitshift(A(i,2),-2)];
   end;
   i=i+1;
end;
ANNOT(length(ANNOT))=[];       % last line = EOF (=0)
ATRTIME(length(ATRTIME))=[];   % last line = EOF
clear A;
ATRTIME= (cumsum(ATRTIME))/sfreq;
ind= find(ATRTIME <= TIME(end));
ATRTIMED= ATRTIME(ind);
ANNOT=round(ANNOT);
ANNOTD= ANNOT(ind);
%% vyber delky atr dat
aa=ATRTIME*250;
poz=find(aa>SAMPLES2READ,1);
if (isempty(poz)==1)
    poz=length(ATRTIME);
end
%% ----Exporting-------------------------------------------------------------

ekg(:,1) = M(:,1);
ekg(:,2) = M(:,2);
atr(:,1)=ANNOT(1:(poz-2));
atr(:,2)=ATRTIME(1:(poz-2));
atr(:,3)=aa(1:(poz-2));

end
