function varargout = EKG_gui_v2(varargin)
% EKG_GUI_V2 MATLAB code for EKG_gui_v2.fig
%      EKG_GUI_V2, by itself, creates a new EKG_GUI_V2 or raises the existing
%      singleton*.
%
%      H = EKG_GUI_V2 returns the handle to a new EKG_GUI_V2 or the handle to
%      the existing singleton*.
%
%      EKG_GUI_V2('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in EKG_GUI_V2.M with the given input arguments.
%
%      EKG_GUI_V2('Property','Value',...) creates a new EKG_GUI_V2 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before EKG_gui_v2_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to EKG_gui_v2_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help EKG_gui_v2

% Last Modified by GUIDE v2.5 20-Apr-2013 00:39:20

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @EKG_gui_v2_OpeningFcn, ...
                   'gui_OutputFcn',  @EKG_gui_v2_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before EKG_gui_v2 is made visible.
function EKG_gui_v2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to EKG_gui_v2 (see VARARGIN)

% Choose default command line output for EKG_gui_v2
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);
% set(handles.figure1,'Units','Pixels');
% scrsz = get(0,'ScreenSize'); % ziskani rozmeru obrazovky 
% set(handles.figure1,'Position',[scrsz(1) scrsz(2) scrsz(3) scrsz(4)]);
% set(handles.figure1,'Units','normalized');
% UIWAIT makes EKG_gui_v2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = EKG_gui_v2_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
global QRS segment;  % definice globalni promenny
varargout{1} = QRS;
varargout{2} = segment;


% --- Executes when figure1 is resized.
function figure1_ResizeFcn(hObject, eventdata, handles) 
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% pro zvetsovani

% --- Executes on button press in Load.
function Load_Callback(hObject, eventdata, handles) % tlacitko load
% hObject    handle to Load (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[FileName,PathName] = uigetfile('*.mat','Vyberte soubor se signalem EKG'); % otevre okno se signalama
set(handles.Soubor,'String',FileName);
set(handles.t1,'String',0); % rozsah malych oken, kvuli nacitani dalsiho, kdyby se pred tim priblizovalo
set(handles.t2,'String',5);
FileName = get(handles.Soubor,'String');
global x OSAy OSAy1 QRS C; % definice globalnich promennych
load(FileName);
X = fft(x); % ve vsech svodech pro zobrazeni
X(1) = 0;
x = ifft(X);
rozsah = 0;
for i = 1:12 % zjisteni dynamiky svodu, podle nejvetsi se udelaji parametry pro osy
    rozsahy = max(x(i,:)) - min(x(i,:));
    if rozsah < rozsahy
        OSAy = rozsahy;
        OSAy1 = OSAy;
    end;
end;
set(handles.odstup,'String',OSAy);
QRS = 0;
C = 0;
set(handles.QRS,'Enable','on'); % jak se nacte signal, tak se zpristupni...
set(handles.Okno,'Enable','on');
set(handles.Mereni,'Enable','on');
set(handles.Cluster,'Enable','on');
set(handles.RRcheck,'Enable','off');
set(handles.RRcheck,'BackgroundColor',[0.941 0.941 0.941]);
set(handles.figure1,'Name',FileName);
set(handles.RRcheck,'String','Overit RR');
set(handles.RRtest,'BackgroundColor',[1 1 1]); 
set(handles.RRtest2,'BackgroundColor',[1 1 1])
Zobraz_Callback(hObject, eventdata, handles); % volani aktualizace obrazovky

% --- Executes on button press in Zobraz.
function Zobraz_Callback(hObject, eventdata, handles) % aktuazace obrazovky
% hObject    handle to Zobraz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global x OSAy freq QRS C prumRR;
test(1:10) = 0; % kalibracni impuls po castech od - do vzorku
test(11:50) = 1;
test(51:90) = 0.5;
test(91) = 0;
freq = get(handles.Freq,'String'); % fvz
freq = str2double(freq);
t1 = get(handles.t1,'String');% hodnota z oknyka
t1 = str2double(t1); % udelej z toho cislo
t2 = get(handles.t2,'String');
t2 = str2double(t2);
OSAx = 0:1/freq:(length(x)-1)/freq; % vypocet cisel do spodni osy - cas
OSAxtest = OSAx(1:91);

OSAy = get(handles.odstup,'String');
OSAy = str2double(OSAy);

plot(handles.axesI,OSAx,x(1,:)/1000,OSAx,(x(2,:)-OSAy)/1000,... % prvni okno
    OSAx,(x(3,:)-2*OSAy)/1000,OSAx,(x(4,:)-3*OSAy)/1000,OSAx,(x(5,:)-4*OSAy)/1000,...
    OSAx,(x(6,:)-5*OSAy)/1000,OSAxtest,test,OSAxtest,test-OSAy/1000,...
    OSAxtest,test-2*OSAy/1000,OSAxtest,test-3*OSAy/1000,OSAxtest,test-4*OSAy/1000,...
    OSAxtest,test-5*OSAy/1000);
yLmin(1) = min(x(6,:)-5*OSAy)/1000;
yLmin(2) = min(x(12,:)-5*OSAy)/1000;
yLmin(3) = min(test-5*OSAy)/1000;
yL(1) = min(yLmin);
yLmax(1) = max(x(1,:))/1000;
yLmax(2) = max(x(7,:))/1000;
yLmax(3) = max(test);
yL(2) = 1.1*max(yLmax);
set(handles.axesI,'XLim',([t1 t2]),'XTick',0:1/25:t2,'YLim',yL,'YTick',yL(1):1/10:yL(2));
ylabel(handles.axesI,'Amplituda [mV]','Color','k');
xL = get(handles.axesI,'XLim');
xT = get(handles.axesI,'XTick');


if QRS ~= 0 % kresleni sipek na qrs
set(handles.axesI,'NextPlot','add');
QRSa = (max(x(1,:))/1000)*(ones(1,length(QRS)));
s1 = stem(handles.axesI,QRS/freq,QRSa,'fill');
set(s1,'MarkerFaceColor','red','LineStyle','none','Marker','v',...
    'MarkerSize',5)
set(get(s1,'Baseline'),'Visible','off')
set(handles.axesI,'NextPlot','ReplaceChildren');
% BPM = length(QRS)/(length(x)/freq)*60; % vypocet BPM
BPM = 60./prumRR; % vypocet BPM
set(handles.BPM,'String',BPM);
end;


plot(handles.axesV1,OSAx,x(7,:)/1000,OSAx,(x(8,:)-OSAy)/1000,... % zobrazeni vpravo nahore
    OSAx,(x(9,:)-2*OSAy)/1000,OSAx,(x(10,:)-3*OSAy)/1000,OSAx,(x(11,:)-4*OSAy)/1000,...
    OSAx,(x(12,:)-5*OSAy)/1000,OSAxtest,test,OSAxtest,test-OSAy/1000,...
    OSAxtest,test-2*OSAy/1000,OSAxtest,test-3*OSAy/1000,OSAxtest,test-4*OSAy/1000,...
    OSAxtest,test-5*OSAy/1000)
set(handles.axesV1,'XLim',xL,'XTick',xT,'YLim',yL,'YTick',yL(1):1/10:yL(2));

if QRS ~= 0
set(handles.axesV1,'NextPlot','add');
QRSa = (max(x(7,:))/1000)*(ones(1,length(QRS)));
s2 = stem(handles.axesV1,QRS/freq,QRSa,'fill');
set(s2,'MarkerFaceColor','red','LineStyle','none','Marker','v',...
    'MarkerSize',5)
set(get(s2,'Baseline'),'Visible','off')
set(handles.axesV1,'NextPlot','ReplaceChildren');
end;

svod = get(handles.svod,'Value'); % vyber zobrazeni svodu do ref.
SvodT = get(handles.svod,'String');
SvodT = SvodT{svod};
set(handles.svodT,'String',SvodT);
plot(handles.axesall,OSAx,x(svod,:)/1000)
set(handles.axesall,'YLim',...
    ([min(x(svod,:))/1000 1.1*max(x(svod,:))/1000]),...
    'XTick',0:0.5:length(x(1,:))/200,'YTick',yL(1):1/10:yL(2));
xlabel(handles.axesall,'Cas [s]','Color','k');

if QRS ~= 0
set(handles.axesall,'NextPlot','add');
QRSa = (max(x(svod,:))/1000)*(ones(1,length(QRS)));
s3 = stem(handles.axesall,QRS/freq,QRSa,'fill');
set(s3,'MarkerFaceColor','red','LineStyle','none','Marker','v',...
    'MarkerSize',5)
set(get(s3,'Baseline'),'Visible','off')
set(handles.axesall,'NextPlot','ReplaceChildren');
end;

if length(C) > 1 % vykresleni cisel komplexu a cisla shluku
set(s3,'LineStyle','none','Marker','none')
Stemlabel = num2cell(C);
Stemlabel2 = 1:1:length(QRS);
Stemlabel2 = num2cell(Stemlabel2);
Xd = get(s3,'XData');
Yd = get(s3,'YData');
set(0,'CurrentFigure',handles.figure1)
subplot(handles.axesall);
for K = 1 : length(Xd)
    text(Xd(K)+0.1,Yd(K),Stemlabel(K),'HorizontalAlignment','center',...
        'Color','b');
    text(Xd(K),Yd(K),'/','HorizontalAlignment','center',...
        'Color','k');
    text(Xd(K)-0.1,Yd(K),Stemlabel2(K),'HorizontalAlignment','center',...
        'Color','r');
end;
end;

% --------------------------------------------------------------------
function File_Callback(hObject, eventdata, handles)
% hObject    handle to File (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



function Freq_Callback(hObject, eventdata, handles)
% hObject    handle to Freq (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



% --- Executes during object creation, after setting all properties.
function Freq_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Freq (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in Okno.
function Okno_Callback(hObject, eventdata, handles) % pro tlacitko onko, nastaveni priblizeni + mysi
% hObject    handle to Okno (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[polohax,~] = ginput(2);
set(handles.t1,'String',polohax(1));
set(handles.t2,'String',polohax(2));
Zobraz_Callback(hObject, eventdata, handles);


% --- Executes on button press in QRS.
function QRS_Callback(hObject, eventdata, handles) % tlacitko qrs
% hObject    handle to QRS (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global x QRS vzdalenostRR freq prumRR;
fvz = get(handles.Freq,'String');
fvz = str2double(fvz);
QRS = QRSdetektor_global(fvz,x,'c','n'); % spusti funkci qrs detektor
% set(handles.Cluster,'Enable','on');
set(handles.RRcheck,'Enable','on'); % odted jde merit RR interval
vzdalenostRR = zeros(1,length(QRS)-1);
for i = 1:length(QRS)-1
     vzdalenostRR(i) = QRS(i+1)-QRS(i);
end;
prumRR = (sum(vzdalenostRR)/length(vzdalenostRR))/freq;
set(handles.RR,'String',prumRR); %prumerny RR int. v sec
Zobraz_Callback(hObject, eventdata, handles); % zobrazi spicky


% --- Executes on selection change in svod.
function svod_Callback(hObject, eventdata, handles) % okno vyberu svodu
% hObject    handle to svod (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
Zobraz_Callback(hObject, eventdata, handles);
% Hints: contents = cellstr(get(hObject,'String')) returns svod contents as cell array
%        contents{get(hObject,'Value')} returns selected item from svod


% --- Executes during object creation, after setting all properties.
function svod_CreateFcn(hObject, eventdata, handles)
% hObject    handle to svod (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function odstup_Callback(hObject, eventdata, handles)
% hObject    handle to odstup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
Zobraz_Callback(hObject, eventdata, handles);
% Hints: get(hObject,'String') returns contents of odstup as text
%        str2double(get(hObject,'String')) returns contents of odstup as a double


% --- Executes during object creation, after setting all properties.
function odstup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to odstup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in Mereni.
function Mereni_Callback(hObject, eventdata, handles) % mereni
% hObject    handle to Mereni (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[xmer,~] = ginput(2);
roz = abs(xmer(2)-xmer(1));
set(handles.rozdil,'String',roz);


function rozdil_Callback(hObject, eventdata, handles)
% hObject    handle to rozdil (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of rozdil as text
%        str2double(get(hObject,'String')) returns contents of rozdil as a double


% --- Executes during object creation, after setting all properties.
function rozdil_CreateFcn(hObject, eventdata, handles)
% hObject    handle to rozdil (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in Reset.
function Reset_Callback(hObject, eventdata, handles)
% hObject    handle to Reset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global OSAy1;
set(handles.t1,'String',0);
set(handles.t2,'String',5);
set(handles.odstup,'String',OSAy1);
Zobraz_Callback(hObject, eventdata, handles);


% --- Executes on button press in Cluster.
function Cluster_Callback(hObject, eventdata, handles) % shlukovka
% hObject    handle to Cluster (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global QRS x C freq;
if QRS == 0 % kdy nebyly detekovany qrs, tak je detekuj kdyz budes delat cluster
    QRS_Callback(hObject, eventdata, handles);
end;
vyberQRS = get(handles.vyberQRS,'Value'); % moznosti nastaveni shukovky v gui
clus1 = get(handles.ClusterS,'String');
clus1 = str2double(clus1);
clus2 = get(handles.ClusterS2,'String');
clus2 = str2double(clus2);
svod = get(handles.svod,'Value');
k = get(handles.metod,'Value');
num = get(handles.num,'String');
num = str2double(num);
repl = get(handles.replicants,'String');
repl = str2double(repl);
auto = get(handles.AutoNum,'Value');
drift = get(handles.drift,'Value');
f = get(handles.Filtr,'String');
f = str2double(f);
FDP = get(handles.FDP,'Value');

if FDP == 1
    N=51;
    f=fir1(N,f/freq,'low');
    % zpozdeni=(N-1)/2;
    xf(svod,:)=filter(f,1,x(svod,:)); % vyfiltrovanych 12 svodu
    % filt_sig2(i,:)=filt_sig(i,zpozdeni+1:end); % vyfiltrovanych 12 svodu
    % se zpozdenim
else
    xf(svod,:) = x(svod,:);
end;
if k == 1 % vyber typu shlukovky a co se u nich ma zobrazovat na vyber
    i = get(handles.dist,'Value');
    j = get(handles.dist,'String');
    metric = j{i};
    i = get(handles.link,'Value');
    j = get(handles.link,'String');
    link = j{i};
    C = clusteran(QRS,xf,vyberQRS,clus1,clus2,svod,metric,link,num,auto,drift,freq); % spusti clusteran
else
    i = get(handles.dist,'Value');
    j = get(handles.dist,'String');
    metric = j{i};
    C = kmeansmy(QRS,xf,vyberQRS,clus1,clus2,svod,metric,num,repl,drift,auto,freq); % spusti kmeans
end;
Zobraz_Callback(hObject, eventdata, handles);


% --- Executes on selection change in vyberQRS.
function vyberQRS_Callback(hObject, eventdata, handles)
% hObject    handle to vyberQRS (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


switch get(handles.vyberQRS,'Value'); % 3 typy vyberu QRS
    case 1
        set(handles.ClusterS,'String',10)
        set(handles.ClusterS,'Enable','on')
        set(handles.ClusterS2,'String',10)
        set(handles.ClusterS2,'Enable','on')
        set(handles.ClusterJ,'String','%')
    case 2
        set(handles.ClusterS,'String',100)
        set(handles.ClusterS,'Enable','on')
        set(handles.ClusterS2,'String',100)
        set(handles.ClusterS2,'Enable','on')
        set(handles.ClusterJ,'String','msec')
    case 3
        set(handles.ClusterS,'String','-')
        set(handles.ClusterS,'Enable','off')
        set(handles.ClusterS2,'String','-')
        set(handles.ClusterS2,'Enable','off')
        set(handles.ClusterJ,'String','-')
end;
% Hints: contents = cellstr(get(hObject,'String')) returns vyberQRS contents as cell array
%        contents{get(hObject,'Value')} returns selected item from vyberQRS


% --- Executes during object creation, after setting all properties.
function vyberQRS_CreateFcn(hObject, eventdata, handles)
% hObject    handle to vyberQRS (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in popisky.
function popisky_Callback(hObject, eventdata, handles)
% hObject    handle to popisky (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of popisky
vis = get(handles.popisky,'Value');
if vis == 0
    set(handles.text4,'Visible','off');
    set(handles.text5,'Visible','off');
    set(handles.text6,'Visible','off');
    set(handles.text7,'Visible','off');
    set(handles.text8,'Visible','off');
    set(handles.text9,'Visible','off');
    set(handles.text10,'Visible','off');
    set(handles.text11,'Visible','off');
    set(handles.text12,'Visible','off');
    set(handles.text13,'Visible','off');
    set(handles.text14,'Visible','off');
    set(handles.text15,'Visible','off');
    set(handles.svodT,'Visible','off');
elseif vis == 1
    set(handles.text4,'Visible','on');
    set(handles.text5,'Visible','on');
    set(handles.text6,'Visible','on');
    set(handles.text7,'Visible','on');
    set(handles.text8,'Visible','on');
    set(handles.text9,'Visible','on');
    set(handles.text10,'Visible','on');
    set(handles.text11,'Visible','on');
    set(handles.text12,'Visible','on');
    set(handles.text13,'Visible','on');
    set(handles.text14,'Visible','on');
    set(handles.text15,'Visible','on');
    set(handles.svodT,'Visible','on');
end;



function ClusterS_Callback(hObject, eventdata, handles)
% hObject    handle to ClusterS (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ClusterS as text
%        str2double(get(hObject,'String')) returns contents of ClusterS as a double


% --- Executes during object creation, after setting all properties.
function ClusterS_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ClusterS (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function BPM_Callback(hObject, eventdata, handles)
% hObject    handle to BPM (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of BPM as text
%        str2double(get(hObject,'String')) returns contents of BPM as a double


% --- Executes during object creation, after setting all properties.
function BPM_CreateFcn(hObject, eventdata, handles)
% hObject    handle to BPM (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in dist.
function dist_Callback(hObject, eventdata, handles)
% hObject    handle to dist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns dist contents as cell array
%        contents{get(hObject,'Value')} returns selected item from dist


% --- Executes during object creation, after setting all properties.
function dist_CreateFcn(hObject, eventdata, handles)
% hObject    handle to dist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in link.
function link_Callback(hObject, eventdata, handles)
% hObject    handle to link (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns link contents as cell array
%        contents{get(hObject,'Value')} returns selected item from link


% --- Executes during object creation, after setting all properties.
function link_CreateFcn(hObject, eventdata, handles)
% hObject    handle to link (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function num_Callback(hObject, eventdata, handles)
% hObject    handle to num (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of num as text
%        str2double(get(hObject,'String')) returns contents of num as a double


% --- Executes during object creation, after setting all properties.
function num_CreateFcn(hObject, eventdata, handles)
% hObject    handle to num (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in metod.
function metod_Callback(hObject, eventdata, handles)
% hObject    handle to metod (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.dist,'Value',1); % pri prepnuti metody prepsat okynka
set(handles.link,'Value',1);
if get(handles.metod,'Value') == 1
    D = {'euclidean';'seuclidean';'cityblock';'minkowski';'chebychev';...
        'mahalanobis';'cosine';'correlation';'spearman'};
    set(handles.dist,'String',D);
    D = {'single';'average';'centroid';'complete';'median';'ward';...
        'weighted'};
    set(handles.link,'String',D);
    set(handles.link,'Visible','on');
    set(handles.replicants,'Visible','off');
    set(handles.text25,'Visible','off');
    set(handles.AutoNum,'Visible','on');
    set(handles.AutoNum,'Value',0);
    set(handles.num,'Visible','on');
    set(handles.text24,'Visible','on');
else
    D = {'sqEuclidean';'cityblock';'cosine';'correlation'};
    set(handles.dist,'String',D);
    set(handles.link,'Visible','off');
    set(handles.replicants,'Visible','on');
    set(handles.text25,'Visible','on');
    set(handles.AutoNum,'Visible','on');
    set(handles.AutoNum,'Value',0);
    set(handles.num,'Visible','on');
    set(handles.text24,'Visible','on');
end;
% Hints: contents = cellstr(get(hObject,'String')) returns metod contents as cell array
%        contents{get(hObject,'Value')} returns selected item from metod


% --- Executes during object creation, after setting all properties.
function metod_CreateFcn(hObject, eventdata, handles)
% hObject    handle to metod (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function replicants_Callback(hObject, eventdata, handles)
% hObject    handle to replicants (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of replicants as text
%        str2double(get(hObject,'String')) returns contents of replicants as a double


% --- Executes during object creation, after setting all properties.
function replicants_CreateFcn(hObject, eventdata, handles)
% hObject    handle to replicants (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function t1_Callback(hObject, eventdata, handles)
% hObject    handle to t1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of t1 as text
%        str2double(get(hObject,'String')) returns contents of t1 as a double


% --- Executes during object creation, after setting all properties.
function t1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to t1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function t2_Callback(hObject, eventdata, handles)
% hObject    handle to t2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of t2 as text
%        str2double(get(hObject,'String')) returns contents of t2 as a double


% --- Executes during object creation, after setting all properties.
function t2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to t2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in AutoNum.
function AutoNum_Callback(hObject, eventdata, handles) %chneckbox na auto shluku
% hObject    handle to AutoNum (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(handles.AutoNum,'Value') == 1
    set(handles.num,'Visible','off');
    set(handles.text24,'Visible','off');
elseif get(handles.AutoNum,'Value') == 0
    set(handles.num,'String',2);
    set(handles.num,'Visible','on');
    set(handles.text24,'Visible','on');
end;
% Hint: get(hObject,'Value') returns toggle state of AutoNum



function ClusterS2_Callback(hObject, eventdata, handles)
% hObject    handle to ClusterS2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ClusterS2 as text
%        str2double(get(hObject,'String')) returns contents of ClusterS2 as a double


% --- Executes during object creation, after setting all properties.
function ClusterS2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ClusterS2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function RR_Callback(hObject, eventdata, handles)
% hObject    handle to RR (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of RR as text
%        str2double(get(hObject,'String')) returns contents of RR as a double


% --- Executes during object creation, after setting all properties.
function RR_CreateFcn(hObject, eventdata, handles)
% hObject    handle to RR (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function RRtest_Callback(hObject, eventdata, handles)
% hObject    handle to RRtest (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of RRtest as text
%        str2double(get(hObject,'String')) returns contents of RRtest as a double


% --- Executes during object creation, after setting all properties.
function RRtest_CreateFcn(hObject, eventdata, handles)
% hObject    handle to RRtest (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in RRcheck.
function RRcheck_Callback(hObject, eventdata, handles)
% hObject    handle to RRcheck (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global vzdalenostRR freq;
QRS_Callback(hObject, eventdata, handles);
vzdalenostRR = vzdalenostRR/freq;
RRtest = str2double(get(handles.RRtest,'String')); % to vepisovani procent
RRtest2 = str2double(get(handles.RRtest2,'String'));
testing = 0;
testing2 = 0;
for i = 1:length(vzdalenostRR)-1
if vzdalenostRR(i+1) > (1+RRtest/100)*vzdalenostRR(i)
    testing = 1;
end;
if vzdalenostRR(i+1) < (1-RRtest2/100)*vzdalenostRR(i)
    testing2 = 1;
end;
end;
testing3 = testing + testing2;
switch testing3
    case 2;
        set(handles.RRcheck,'BackgroundColor','r');
        set(handles.RRtest,'BackgroundColor','r');
        set(handles.RRtest2,'BackgroundColor','r');
    case 1;
        if testing2 == 1;
            set(handles.RRcheck,'BackgroundColor','r');
            set(handles.RRtest2,'BackgroundColor','r');
            set(handles.RRtest,'BackgroundColor','g');
        else
            set(handles.RRcheck,'BackgroundColor','r');
            set(handles.RRtest2,'BackgroundColor','g');
            set(handles.RRtest,'BackgroundColor','r');
        end;
    case 0;
        set(handles.RRcheck,'BackgroundColor','g');
        set(handles.RRtest,'BackgroundColor','g');
        set(handles.RRtest2,'BackgroundColor','g');
        set(handles.RRcheck,'String','OK');
end;


function RRtest2_Callback(hObject, eventdata, handles)
% hObject    handle to RRtest2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of RRtest2 as text
%        str2double(get(hObject,'String')) returns contents of RRtest2 as a double


% --- Executes during object creation, after setting all properties.
function RRtest2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to RRtest2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in drift.
function drift_Callback(hObject, eventdata, handles)
% hObject    handle to drift (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of drift



function Filtr_Callback(hObject, eventdata, handles)
% hObject    handle to Filtr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Filtr as text
%        str2double(get(hObject,'String')) returns contents of Filtr as a double


% --- Executes during object creation, after setting all properties.
function Filtr_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Filtr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in FDP.
function FDP_Callback(hObject, eventdata, handles)
% hObject    handle to FDP (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(handles.FDP,'Value') == 0
    set(handles.Filtr,'Visible','off');
    set(handles.text28,'Visible','off');
elseif get(handles.FDP,'Value') == 1
    set(handles.Filtr,'String',70);
    set(handles.Filtr,'Visible','on');
    set(handles.text28,'Visible','on');
end;
% Hint: get(hObject,'Value') returns toggle state of FDP
