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

% Last Modified by GUIDE v2.5 08-May-2017 22:50:21

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @EKG_GUI_OpeningFcn, ...
                   'gui_OutputFcn',  @EKG_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 EKG_GUI is made visible.
function EKG_GUI_OpeningFcn(hObject, eventdata, handles, varargin)
global s zoom stop soubor u RR_min RR_max RR_AVE1 poloha_peaku fvz pocet_vzorku_x ref_perioda interval max_y prumer_osy_y hystogram pro_tep hist_tep histo tepova_frekvence start
interval = 1; % interval pro vý počet tf nemění se 
fvz = 1000; % V Hz
pocet_vzorku_x = 3000; % Tyto vzorky slouží jako okno pro výpočet TF
max_y = 0;
prumer_osy_y = 0;
hystogram = 'Nic';  % Tvorba proměnné pro zobrazení tepové frekvence v čase
pro_tep = 1;        % Pomocné proměnná pro ukládání tepové frekvence
hist_tep = 1;       % Pomocná proměnná pro závislost tepové frekvence na čase
tepova_frekvence = 1;

% 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 (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = EKG_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 slider movement.
function slider1_Callback(hObject, eventdata, handles)
global zoom 
% hObject    handle to slider1 (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
zoom = round(get(handles.slider1,'value'));

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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
global s vyber_pripojeni stop soubor u RR_min RR_max RR_AVE1 poloha_peaku pocet_vzorku_x fvz ref_perioda interval max_y prumer_osy_y hist_tep pro_tep hystogram tepova_frekvence start
% 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)
vyber_zvire = get(handles.popupmenu3,'Value');
vyber_pripojeni = get(handles.popupmenu2,'Value');
nazev_zarizeni = get(handles.edit1,'String');
switch vyber_pripojeni
    case 1              % přpiojení pomocí bluetooth
        set(handles.text4,'String','Chyba č.1: Špatný název zařízení, nebo COM! 1.Odpojte zařízení 2.Restartujte aplikaci a napište správný název zařízení')
        s = Bluetooth(nazev_zarizeni,1);
        fopen(s);
        set(handles.text4,'String','Připojeno')
        set(handles.togglebutton1,'Enable','off')
%         fscanf(s)  % fscanf změna, zde být nemá!

    case 2              % připojení pomocí USB
        set(handles.text4,'String','Chyba č.1: Špatný název zařízení, nebo COM! Restartujte aplikaci a napište správný název zařízení')
        s = serial(nazev_zarizeni);
        set(s,'BaudRate', 57600);
        fopen(s);
        set(handles.text4,'String','Připojeno')   
end
help = 1;
while (1) % Inicializace připojení 
    a = sscanf((fscanf(s)),'%s');
    switch a
        case 'Wait'
       fwrite(s,1,'int8');
        break
    end
    
   if help == 10
       break % Omezení délky čekání
   end
      help = help + 1

end           % zde změna while smyčka mimo  
set(handles.pushbutton1,'Enable','off')
fscanf(s);
flushinput(s)   % Vymazání vstupního bufferu
pause(0.2)
while 512 > s.BytesAvailable
end
stop = 0;
% 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)
%% Vlastní EKG program; Inicializace hodnot
load look_up_table; % Načtení souboru s převodní tabulkou (Napětí baterie -> procenta)
% fvz = 1000;
% pocet_vzorku_x = 3000; % Tyto vzorky slouží jako okno pro výpočet TF
omezeni = 1;    % omezovac chyby baterie
baterka = 0;    % ovladani baterie
rr_int = 0;     % inicializace intervalu
rr_int2 = 0;    % inicializace intevalu
pom = 1;        % pomocne počitadlo   
u = 1;          % rr_ int index
z = 1;          % Zoom index
v = 1;          % pocitadlo
zesileni = 1;
hl_pocitadlo = 1;
zpetne_hledani = 'zatim_nic_nehledam';
osa = 1000;
fscanf(s)  
%% Akvizice dat 
    set(handles.text4,'String','Připojeno - Běh')
    set(handles.text4,'BackgroundColor','g')
while (1)
    %% Čtení dat z Arduina
    while (1)
        zoom = round(get(handles.slider1,'value')); 
        nahravani = get(handles.checkbox2,'value');
        while z <=zoom      % Pro natavení zoomu
            switch stop
                case 1
                    while (stop==1)
                        
                    end
                  
            end
            volty = (3/1023) * (str2num(fscanf(s))-511.17);     % přepočet vzorků na napěŤovou hodnotu
            prepocet = (volty/zesileni)*1000;
            zobrazeni_h{z} = prepocet;
            y_h{pom} = zobrazeni_h{z};
            zaznam_tep{pom} = tepova_frekvence;
                    switch nahravani        % Nahrávání hodnot do souboru
                        case 1
                        fprintf(soubor,'%i',y_h{pom});
                        fprintf(soubor,'%s',';');
                        fprintf(soubor,'%i\r\n',zaznam_tep{pom});
                        set(handles.text4,'String','Připojeno - Nahrávám')
                        set(handles.text4,'BackgroundColor','g')
                    end
            if u > 9
            hist_tep(pro_tep) = tepova_frekvence;
            pro_tep = pro_tep + 1;
            end
            z = z + 1;      % Počítadlo zobrazení
            pom = pom + 1;  % Pomocné počítadlo pro počet vzorků
        end
       if u > 9
           switch hystogram
               case 'Otevren'
                histo=figure(1);
                plot(hist_tep)
                hysto = gca;
                hysto.XLabel.String = 'Čas [s]';
                hysto.YLabel.String = 'BPM';
                hysto.Title.String =  'Tepová frekvence';
                osa_x = hysto.XTick;
                hysto.XTickLabel = osa_x/1000;
                hysto.FontSize = 23;
                drawnow
               case 'Zavren'
                   close(histo)
                   hystogram = 'Nic'
               case 'Nic'
           end
           
        end
        zobrazeni = cell2mat(zobrazeni_h);
        y = cell2mat(y_h);

        %% Filtrace 50 Hz
        filtr = get(handles.checkbox1,'Value'); 
        switch filtr
            case 1
        dolni_f = 49.5;     % Filtrace pouze pro zobrazení/nahrávání
        horni_f = 50.5;
        rad_f = 2;
        Wn = [dolni_f/(fvz/2) horni_f/(fvz/2)];
        [b,a] = butter(rad_f,Wn,'stop');
        zobrazeni = filtfilt(b,a,zobrazeni);
            otherwise
        end
        %% Vykreslení
        try 
      plot(handles.axes1,zobrazeni,'r','LineWidth',2);
      axis (handles.axes1,[0 length(zobrazeni) -inf inf]);
      osa_x = handles.axes1.XTick;
      osa_y = handles.axes1.YTick;
      handles.axes1.XTickLabel = osa_x/1000;
      handles.axes1.XLabel.String = 'Čas [s]';
      if 965 < osa
      handles.axes1.YLabel.String = 'Napětí [mV]';
      end
      grid(handles.axes1,'on')
      grid(handles.axes1,'minor')
        catch ME
            set(handles.text4,'String','Chyba č.3: Jejda něco se pokazilo :(')
            set(handles.text4,'BackgroundColor','y')
        end
        drawnow
        z = 1;zobrazeni = [];zobrazeni_h = {};
        if pom >= pocet_vzorku_x         % Kontrola dosažení 4s záznamu
            pom = 1;
%             y = y(1:pocet_vzorku_x);
            break
        end
    end
%% Detekce R vlny
dolni_f = 5;
horni_f = 15;
rad_f = 2;
Wn = [dolni_f/(fvz/2) horni_f/(fvz/2)];
[b,a] = butter(rad_f,Wn,'bandpass');
signal = filtfilt(b,a,y);
derivace = diff(signal);
mocnina = derivace.^2;
delka_okna = 110;    % udava se pocet vzorku; delka trvani QRS
    for i = 1:length(mocnina)-delka_okna
        integrace(i) = sum(mocnina(i:i+delka_okna));
    end

dolni_f = 5;
horni_f = 15;
rad_f = 2;
Wn = [dolni_f/(fvz/2) horni_f/(fvz/2)];
[b,a] = butter(rad_f,Wn,'bandpass');        
signal = filtfilt(b,a,y);       % Filtrace
derivace = diff(signal);
mocnina = derivace.^2;
delka_okna = 110;    % udava se pocet vzorku integracniho okna
    for int = 1:length(mocnina)-delka_okna
        integrace(int) = sum(mocnina(int:int+delka_okna));
    end
p = 1;  % Pocitadlo R_vln
i = 1;  % Inkrement hlavni smycky
pomocne_pocitadlo = 1;

    Prah_signal = 0.125*max(integrace);    % Prah signalu
    Prah = Prah_signal;                    % Prah signalu
    Prah_sum = 0.125*mean(integrace);   % Prah sumu

while i<length(integrace)- delka_okna
    if integrace(i)> Prah
        okno = integrace(i:i+delka_okna);
        Max_okno = max(okno);
        Sum_okno = mean(okno);
        poloha_peaku_pom = find(integrace==Max_okno);
            if poloha_peaku_pom == 1 || poloha_peaku_pom == 2
                 poloha_peaku_pom = 3;         
            else
                dolni_mez = integrace(poloha_peaku_pom - 1);  
                horni_mez = integrace(poloha_peaku_pom + 1);   
            if dolni_mez <= Max_okno && horni_mez <= Max_okno   
                poloha_peaku(p) = poloha_peaku_pom;
            if p >= 2
                rr_int(u) = abs(diff(poloha_peaku(end-1:end)));     % Vektror hodnot RR intervalů
                rr_int_help = abs(diff(poloha_peaku(end-1:end)));   % Poslední RR interval
                u = u+1;
          
            end    
                hodnota_peaku(p) = integrace(poloha_peaku(p)); 
                Prah_signal = hodnota_peaku(end);    % Odhad amplitudy dalsiho peaku
                switch zpetne_hledani         % Zpetne hledani
                     case    'Hledam'
                        Prah_signal = 0.25 * Max_okno + 0.75 * Prah_signal;  % Odhad peaku pro zpentne hledani
                        zpetne_hledani = 'nasel';
                     otherwise
%% Rovnice pro výpočet prahových hodnot
                        Prah_signal = 0.125 * Max_okno + 0.8 * Prah_signal;    
                        Prah_sum = 0.125 * Sum_okno + 0.875 * Prah_sum;
                        Prah = Prah_sum + 0.25 * (Prah_signal - Prah_sum);
                end
                p = p+1;
                pomocne_pocitadlo = 1;              
           end
        end
        i = i+ref_perioda*fvz;     % Refrakterni perioda 
    end
    if u < 9 % učební fáze
        RR_min = 0.92 * mean(rr_int);
        RR_max = 1.16 * mean(rr_int);
        RR_AVE1 = mean(rr_int);
    end
    try
    if u > 9    % systém naučen
        if RR_min <= rr_int_help &&  rr_int_help <= RR_max % výpočet validní RR 
            if p > 2
            rr_int2(v) = rr_int_help;   % zápis validní rr do vektoru
            rr_int_tf(interval) = rr_int_help
            RR_AVE1 = mean(rr_int2);    % výpočet střední hodnoty intervalu;
            RR_min = 0.92 * mean(rr_int2)   % minimum
            RR_max = 1.16 * mean(rr_int2)   % maximum
            rr_int_help = 0;
            v = v + 1;
            interval = interval + 1;
            end
        end
    
        if pomocne_pocitadlo > RR_AVE1*1.26     % Zpětné hledání R vlny při překročení 1,26*mean
            if length(poloha_peaku) > 0
                i = poloha_peaku(end);
            end

        Prah = Prah * 0.5;
        zpetne_hledani = 'Hledam';
        pomocne_pocitadlo = 1;
            end
     
    end
    catch ME
        set(handles.text4,'String','Chyba č.2: Špatná fáze učení, zopakujte! Zkontrolujte volbu organismu! V případě, že chyba přetrvává obraťe se na vývojáře')
        set(handles.text4,'BackgroundColor','r')
    end

i = i+1;
pomocne_pocitadlo = pomocne_pocitadlo + 1;
if interval == 3
    interval = 1;
end
    if v == 8
        v = 1;
    end
end
%% Výpočet tepové frekvence
try
if u > 9
     rozdil = mean(rr_int_tf);
     prepocet  = rozdil/fvz;
     tepova_frekvence = round(60/mean(prepocet),0)
     set(handles.text8,'String',tepova_frekvence)
else
    set(handles.text8,'String','Ucim se')
    set(handles.text4,'BackgroundColor','r')
end
catch ME
    set(handles.text4,'String','Chyba č.2: Špatná fáze učení, zopakujte! Zkontrolujte volbu organismu! V případě, že chyba přetrvává obraťe se na vývojáře')
    set(handles.text4,'BackgroundColor','r')
end
%% Nulovani promennych
    poloha_peaku = [];
    hodnota_peaku = [];
    rozdil_vektor = [];
    integrace = [];
    y = [];
    y_h = {};
    z = 1;
    zaznam_tep = [];
    hl_pocitadlo = hl_pocitadlo + 1;
%% Detekce stavu baterie
switch baterka
    case 1
        pomoc = 'Chyba baterie';
        set(handles.text7,'String',pomoc);
    otherwise
fprintf(s,'%s','B');    % Odeslání příkazu B
             while (1)
             a = sscanf(fscanf(s),'%s');    % čekání na návěstí BATERRY
                switch a 
                    case 'BATERRY'
                    b = str2num(fscanf(s));   % Druhý vzorek je stav Baterie
                    osa = str2num(fscanf(s))  % Třetí vzorek je vypocet osy y 
                    zesileni = 0.2222*osa^2 - 429.7*osa + 208665;       % Přepočet zesílení dle polynomu
                    text_zesileni = num2str(round(zesileni,0));
                    set(handles.text18,'String',text_zesileni);
                    break
                end
                if omezeni == 2000   % v případě nezaznamenání příkazu baterka po dobu 1s je cyklus čtení stavu baterie ukončen
                    baterka = 1;
                    break
                end
                omezeni = omezeni + 1;
             end
        b=b(1);
        napeti = round( b * (3/1023),2);                % Výpočet napětí ze vzorků 
        index_napeti = find(krivka(:,1)==napeti);       % Hledání odpovídajícího napěí v tabulce
        procenta = krivka(index_napeti,2);              % zobrazení procent 
        set(handles.text7,'String',procenta);
        if 965 > osa
            handles.axes1.YTick = [];
            set(handles.text4,'String','Připojeno - MIMO ROZSAH ZESÍLENÍ');
            handles.axes1.YLabel.String = 'Mimo rozsa';
            set(handles.text4,'BackgroundColor','r');
            set(handles.text18,'BackgroundColor','r');
        else
            set(handles.text4,'String','Připojeno - Běh')
            set(handles.text4,'BackgroundColor','g')
             set(handles.text18,'BackgroundColor','g');
        end
    end
        omezeni = 1;
end
% set(handles.pushbutton3,'Value',1)
% --- 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



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 pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
global s soubor hist_tep pro_tep
% 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)
%% Ukončení sériové linky a uzavření souboru pro záznam
fclose(s) 
clear ('s')
delete('s')
set(handles.text4,'String','Odpojeno')
set(handles.togglebutton1,'Enable','on')
set(handles.pushbutton1,'Enable','on')
if 1 == get(handles.checkbox2,'value');
fclose(soubor);
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

%%


% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
global start
% 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)
%  stop = get(handles.pushbutton4,'Value')
%  start = 0;

% --- 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)


% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
global soubor
% 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
%% Inicializace textového souboru pro záznam
datum = datestr(date);
cas = clock;
hodiny = cas(4);
minuty = cas(5);
hodiny = num2str(hodiny);
minuty = num2str(minuty);
text = '.txt';
datum = strcat(datum,'_',hodiny,'_',minuty,text)
nahravani = get(handles.checkbox2,'Value')
switch nahravani
    case 1
soubor = fopen(datum,'w');
end
fprintf(soubor,'Fany EKG v1.0. , fvz = 1000Hz');
fprintf(soubor,'\r\n');
fprintf(soubor,'ECG_sample;BPM');
fprintf(soubor,'\r\n');


% --- Executes on selection change in popupmenu3.
function popupmenu3_Callback(hObject, eventdata, handles)
global ref_perioda
% hObject    handle to popupmenu3 (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 popupmenu3 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu3
%% Volba refreakterní periody
vyber_zvire = get(handles.popupmenu3,'Value')
switch vyber_zvire
    case 1
        ref_perioda = 250/1000; %člověk [ms]
    case 2
        ref_perioda = 150/1000; %králík [ms]
    case 3
        ref_perioda = 50/1000;  %myš [ms]
end


% --- Executes during object creation, after setting all properties.
function popupmenu3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (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 pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
global hystogram
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
hystogram = 'Otevren';


% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
global hystogram
% 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)
hystogram = 'Zavren';


% --- Executes on button press in togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
global s
% hObject    handle to togglebutton1 (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 togglebutton1
% stop = get(handles.togglebutton1,'Value')
set(handles.text4,'String','Připojeno - PAUZA')

while get(hObject,'Value')
    pause(0.5)
end
flushinput(s)
pause(1)
