function varargout = GUI(varargin)
% GUI MATLAB code for GUI.fig
%      GUI, by itself, creates a new GUI or raises the existing
%      singleton*.
%
%      H = GUI returns the handle to a new GUI or the handle to
%      the existing singleton*.
%
%      GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI.M with the given input arguments.
%
%      GUI('Property','Value',...) creates a new GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before GUI_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to GUI_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 GUI

% Last Modified by GUIDE v2.5 21-Apr-2014 16:12:59

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @GUI_OpeningFcn, ...
                   'gui_OutputFcn',  @GUI_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 GUI is made visible.
function GUI_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 GUI (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);


% UIWAIT makes GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = GUI_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
varargout{1} = handles.output;


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.pushbutton4,'enable','on');
set(handles.pushbutton5,'enable','off');
uiresume
prehravani(hObject, eventdata, handles);


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
uiresume
global pozastavit;
pozastavit=0;
% vymazání hodnot
set(handles.text2,'String','');
set(handles.text3,'String','');
set(handles.text5,'String','');
set(handles.text6,'String','');
set(handles.text10,'String','');
set(handles.checkbox1,'Value',0);
set(handles.checkbox2,'Value',0);
set(handles.checkbox3,'Value',0);
set(handles.checkbox5,'Value',0);
set(handles.popupmenu1,'Value',1);
set(handles.popupmenu2,'Value',1);
cla(handles.axes1);
cla(handles.axes3);
cla(handles.axes4);
cla(handles.axes5);
%nastavení tlačítek
set(handles.checkbox1,'enable','on');
set(handles.pushbutton2,'enable','on');
set(handles.pushbutton3,'enable','on');
set(handles.pushbutton4,'enable','on');
set(handles.pushbutton7,'enable','on');
set(handles.pushbutton5,'enable','off');

% načtení a vykreslení vlastního signálu
data = nacteni_vlastniho(hObject, eventdata, handles);
vykresleni(hObject, eventdata, handles, data);


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
uiresume
global pozastavit;
pozastavit=0;
% vymazání hodnot
set(handles.text2,'String','');
set(handles.text3,'String','');
set(handles.text5,'String','');
set(handles.text6,'String','');
set(handles.text10,'String','');
set(handles.checkbox1,'Value',0);
set(handles.checkbox2,'Value',0);
set(handles.checkbox3,'Value',0);
set(handles.checkbox5,'Value',0);
cla(handles.axes1);
cla(handles.axes3);
cla(handles.axes4);
cla(handles.axes5);
%nastavení tlačítek
set(handles.checkbox1,'enable','on');
set(handles.pushbutton2,'enable','on');
set(handles.pushbutton3,'enable','on');
set(handles.pushbutton4,'enable','on');
set(handles.pushbutton7,'enable','on');
set(handles.pushbutton5,'enable','off');

[data,~] = nacteni_vyberu(hObject, eventdata, handles);
if isempty(data)
    return;
end;
vykresleni(hObject, eventdata, handles, data);  % použití funkce vykresleni

% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (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 popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1


% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (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 popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (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 popupmenu2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu2


% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (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 checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox1 (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 checkbox1
hodnota = get(hObject,'Value');
cesta_k_souboru = get(handles.text2,'String');
    
if isempty(cesta_k_souboru)    
        [data,~] = nacteni_vyberu(hObject, eventdata, handles);
        if isempty(data)
            return;
        end;
        dat=data;
else
        data = importdata(cesta_k_souboru);
        dat=data;
end

if hodnota==1
    cla(handles.axes1);
    cla(handles.axes3);
    [polohy_R,hodnoty_R] = R_detector(data);
    [~,vysledek] = filtr_izo( polohy_R, hodnoty_R, data );
    data(:,2)=vysledek.I;
    data(:,4)=vysledek.II;
    data(:,3)=vysledek.III;
    vykresleni(hObject, eventdata, handles, data) %vykreslení filtrovaných dat
else
    cla(handles.axes1);    
    cla(handles.axes3);
    data=dat;
    vykresleni(hObject, eventdata, handles, data) %vykreslení původních dat
end



% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox2 (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 checkbox2
hodnota = get(hObject,'Value');
cesta_k_souboru = get(handles.text2,'String');
    
if isempty(cesta_k_souboru)    
        [data,~] = nacteni_vyberu(hObject, eventdata, handles);
        if isempty(data)
            return;
        end;
        dat=data;
else
        data = importdata(cesta_k_souboru);
        dat=data;
end

[polohy_R,~] = R_detector(data);
if hodnota==1
    for e=1:(length(polohy_R.I))-1
        vzdal_RR(e)=polohy_R.I(e+1)-polohy_R.I(e);
    end
        int_RR_s=(vzdal_RR./500);
        int_RR_min=60./int_RR_s;
      
    prum_tep=median(int_RR_min);
    prum_tep=round(prum_tep);
    set(handles.text3,'String',prum_tep)
    
else
    set(handles.text3,'String','');
end



% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global pozastavit;
pozastavit=1;


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


% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.pushbutton5,'enable','off');
set(handles.pushbutton4,'enable','on');
uiresume


% --- Executes on button press in checkbox3.
function checkbox3_Callback(hObject, eventdata, handles) %délky os
% hObject    handle to checkbox3 (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 checkbox3
statistika_uhlu_a_delek(hObject, eventdata, handles, 2)



% --- Executes on button press in checkbox5.
function checkbox5_Callback(hObject, eventdata, handles) %úhly
% hObject    handle to checkbox5 (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 checkbox5
statistika_uhlu_a_delek(hObject, eventdata, handles,1)


function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (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 edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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 pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global vymazani;
tlacitko_konec=questdlg('Chcete ukončit vykreslování a vymazat všechny aktuální vstupy?','Ukončení detekce a vymazání hodnot','Ano','Ne','Ne');

switch tlacitko_konec
    case 'Ano'
        vymazani=1;
        uiresume;
            set(handles.text2,'String','');
            set(handles.text3,'String','');
            set(handles.text5,'String','');
            set(handles.text6,'String','');
            set(handles.text10,'String','');
            set(handles.checkbox1,'Value',0);
            set(handles.checkbox2,'Value',0);
            set(handles.checkbox3,'Value',0);
            set(handles.checkbox5,'Value',0);
            set(handles.text5,'String','');
            cla(handles.axes5);
            cla(handles.axes1);
            cla(handles.axes4);
            cla(handles.axes3);
            set(handles.pushbutton1,'enable','on');
            set(handles.pushbutton2,'enable','on');
            set(handles.pushbutton3,'enable','on');
            set(handles.pushbutton4,'enable','on');
            set(handles.pushbutton5,'enable','on');
            set(handles.pushbutton7,'enable','on');
     
    case 'Ne'
        return
end



function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (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 edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (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
