function [f,fvz,u]=load_kralik(fdir,fname,type,length,offset)
% nacitani dat z *.bin z mereni kralicich srdci
% data se pnacitaji ve tvaru "floating point with big-endian" 32/64 bit
%
% [f,fvz,u]=load_kralik(fdir,fname,type,length,offset);
% f ....... nacteny signal
% fvz ..... vzorkovaci frekvence
% u ....... rozsah napeti [Umin, Umax]
%
% fdir .... adresar
% fname ... nazev souboru *.bin
% type .... typ ulozenych dat (single/double)
% length .. delka vystupniho vektoru
%           / (Inf - read to the end of the file)
% offset .. cislo vzorku od ktereho se signal nacte
%           OFFSET values are interpreted as follows:
%         > 0    Move toward the end of the file.
%         = 0    Do not change position.
%
% napriklad:
%     [f,fvz,u]=load_kralik('x:/mypath/','myfile.bin','double',inf,0);
%
% Jan Hrubes, DBME, FEEC, VUT Brno, CZ
% Version 0.2, 9.1.2007

%test input arguments
if (length<1)||(round(length)~=length)
    error('Argument "length" must be positive integer.');
end
if (offset<0)||(round(offset)~=offset)
    error('Argument "offset" must be non-negative integer.');
end
if strcmp(type,'single')
    type2='b';
    type3=4;%*8bitu=32bitu
elseif strcmp(type,'double')
    type2='s';
    type3=8;%*8bitu=64bitu
else error('Argument "type" must be a string "single" or "double".');
end
    
ff=fopen([fdir fname],'r',type2);
fseek(ff, 0, 'eof');
filesize = ftell(ff)/type3; % pocet vzorku v souboru
if (offset+4)>=filesize
    error('Argument offset exceeds file size.');
else
    fseek(ff,0,'bof');
    head=fread(ff,4,type);
      fvz = head(1);
      u = head(2:3);
    fseek(ff,(offset+4)*type3,'bof');
    f=fread(ff,length,type);
      f=f';
    fclose(ff);
    if ((length+offset+4)>filesize)&&(length~=Inf)
        warning('HUGO:changeLength','Arguments (length+offset) exceeds file size, length changed.');
    end
end



