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

% Last Modified by GUIDE v2.5 03-Jun-2008 16:35:22

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Otevrit_OpeningFcn, ...
                   'gui_OutputFcn',  @Otevrit_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 Otevrit is made visible.
function Otevrit_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for Otevrit
handles.output = hObject;
handles.Name=0;
handles.fileput=0; % to check if there's any input
handles.fileput1=0; % to check if there's any input
handles.fileput2=0;
guidata(hObject, handles);

% --- Outputs from this function are returned to the command line.
function varargout = Otevrit_OutputFcn(hObject, eventdata, handles) 

varargout{1} = handles.output;

%--------------------------------------------------------------------------
%               Nazev souboru - zobrazení
%--------------------------------------------------------------------------
function DataFile_Callback(hObject, eventdata, handles)

a=get(hObject,'string');
set(handles.DataFile,'string',a);
handles.fileput=1;
guidata(hObject,handles);


% --- Executes during object creation, after setting all properties.
function DataFile_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%--------------------------------------------------------------------------
%                Výběr souboru z adresářů
%--------------------------------------------------------------------------

% --- Executes on button press in FileBrowse.
function FileBrowse_Callback(hObject, eventdata, handles)
% hObject    handle to FileBrowse (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('*.txt','Select the TXT-file');
TotalFilename=fullfile(PathName,FileName);
set(handles.DataFile,'string',TotalFilename);
handles.fileput=1;
%handles.DataFile=handles.FileBrowse;
guidata(hObject,handles);

function FileBrowsemenu_Callback(hObject, eventdata, handles)
[FileName,PathName] = uigetfile('*.txt','Select the TXT-file');
TotalFilename=fullfile(PathName,FileName);
set(handles.DataFile,'string',TotalFilename);
handles.fileput=1;
guidata(hObject,handles);

%--------------------------------------------------------------------------
%               Indikátor vytvořeného EKG souboru
%--------------------------------------------------------------------------

function indikekg_Callback(hObject, eventdata, handles)
%b=get(hObject,'string');
set(handles.indikekg,'string',b);
handles.fileput1=1;
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function indikekg_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
%--------------------------------------------------------------------------
%                    Tlačítko   Make EKG File
%--------------------------------------------------------------------------

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

File_Name=get(handles.DataFile,'string');
filtrgraf=get(handles.exgraf1,'value')
specgraf=get(handles.exgraf2,'value')
%caszprac=get(handles.info2,'string')
caszprac=0;
[File_Name,caszprac,filtrgraf,specgraf] = EKG_open(File_Name,caszprac,filtrgraf,specgraf);
[pathstr, name, ext, versn] = fileparts(File_Name);
onlyname=joinseq(name,ext);
%fullna=joinseq(pathstr,'\')
%fullna=joinseq(fullna,onlyname);
set(handles.indikekg,'string',onlyname);
set(handles.DataFil,'string',File_Name);
set(handles.info2,'string',caszprac);
%caszprac=get(handles.info2,'string')
%msgbox('Nyní po stisku tlačítka RUN dojde ke klasifikaci.','Tip','help')
guidata(hObject,handles);

%--------------------------------------------------------------------------
%                 Tlačítko -  R U N
%--------------------------------------------------------------------------
% --- Executes on button press in spusti_QRS.
function spusti_QRS_Callback(hObject, eventdata, handles)
c=get(handles.DataFil,'string');
RRinterval=0;
probdet=0;
prenosna=0;
prenosna2=0;
caszprac=0;
timx=0;
Signal=0;
puk=0;
bod=0;
nnet=0;
[File_Name,prenosna,prenosna2,caszprac,timx,Signal,puk,bod,RRinterval,probdet,nnet]...
    = QRS_test(c,prenosna,prenosna2,caszprac,timx,Signal,puk,bod,RRinterval,probdet,nnet);
set(handles.info3,'string',prenosna);
set(handles.TS,'string',prenosna2);
set(handles.nnet,'string',nnet);
set(handles.info2,'string',caszprac);
%plot(timx(1:length(Signal)),Signal);
set(handles.Signal,'string',(Signal));
set(handles.timx,'string',timx);
set(handles.RRinterval,'string',RRinterval);
set(handles.problemy,'string',probdet);
set(handles.puk,'string',puk)


cla reset;
axes(handles.grafcelk);
cla reset;
b=timx(length(timx)-1)+2.5;
axis([0 b min(Signal) max(Signal)*1.2]);
plot(handles.grafcelk,timx(1:length(Signal)),Signal);
hold on;
    for bod=1:length(puk)
    plot(timx(puk(bod)),max(Signal(puk(bod)-5:puk(bod)+5)),'b.', 'MarkerSize',19);
    end


axes(handles.graflupa);
cla reset;
plot(handles.graflupa,timx(1:length(Signal)),Signal);
a=get(handles.posuvka,'Value')*timx(length(timx)-1)-0.2;
b=get(handles.posuvka,'Value')*timx(length(timx)-1)+2.5;
axis([a b min(Signal) max(Signal)*1.2]);
hold on;
for bod=1:length(puk)
plot(timx(puk(bod)),max(Signal(puk(bod)-5:puk(bod)+5)),'b.', 'MarkerSize',29);
tRvlna(bod)=timx(puk(bod));
end
%=========================================================================
        Plot_Title = strcat('EKG_', 2+47 , ' ze souboru : ', File_Name);
        Plot_Title = regexprep(Plot_Title, '_', '-');
        title(Plot_Title);
        xlabel('t [sec]');
        ylabel('U [mV]');
%=========================================================================
%--------------------------------------------------------------------------
%            Informační panel
%--------------------------------------------------------------------------

% --- Executes on selection change in info3.
function info3_Callback(hObject, eventdata, handles)
Signal=get(handles.Signal,'string');
timx=get(handles.timx,'string');
puk=get(handles.puk,'string');
Rvlnbod = (get(hObject,'Value'))-2
indik=str2double(puk(Rvlnbod,:))
    pomoccas=timx(indik,:)
    pomocsig=Signal(indik,:)
    pomocsig=max(str2num(Signal(indik-5:indik+5,:)))
axes(handles.graflupa)
plot(str2num(pomoccas),(pomocsig),'g.', 'MarkerSize',30);
    hold on;
axes(handles.grafcelk)
plot(str2num(pomoccas),(pomocsig),'g.', 'MarkerSize',20);
    hold on;
% --- Executes during object creation, after setting all properties.
function info3_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


%--------------------------------------------------------------------------
%            Informační panel - Čas běhu
%--------------------------------------------------------------------------
function info2_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function info2_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end





%--------------------------------------------------------------------------
%    Neviditelný objekt k indikaci QRSka
%--------------------------------------------------------------------------

function DataFil_Callback(hObject, eventdata, handles)

function DataFil_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function Signal_Callback(hObject, eventdata, handles)

function Signal_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function timx_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function timx_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function puk_Callback(hObject, eventdata, handles)

function puk_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function info4_Callback(hObject, eventdata, handles)

function info4_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


%--------------------------------------------------------------------------
%     ---->>>   P O S U V N I K  ---->>>
%--------------------------------------------------------------------------


% --- Executes on slider movement.
function posuvka_Callback(hObject, eventdata, handles)
%posuvhodnota = get(hObject,'Value')
Signal=str2num(get(handles.Signal,'string'));
timx=str2num(get(handles.timx,'string'));
posuv=(get(handles.posuvka,'Value'));
aa=(posuv*timx(length(timx)-1)-0.5)
bb=((posuv*timx(length(timx)-1))+1.5)
%aa=str2num(aa)
%bb=str2num(bb,10.0)
cc=min((Signal))
dd=max((Signal))*1.2
axes(handles.graflupa);
axis([aa  bb  cc  dd]);
%plot(handles.graflupa,str2num(timx(1:length(Signal))),str2num(Signal));

%hold on;
% --- Executes during object creation, after setting all properties.
function posuvka_CreateFcn(hObject, eventdata, handles)
% hObject    handle to posuvka (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

%--------------------------------------------------------------------------
%                 Š K R T Á T K A
%--------------------------------------------------------------------------

% --- Executes on button press in exgraf1.
function exgraf1_Callback(hObject, eventdata, handles)
filtrgraf=(get(handles.exgraf1,'Value'))


% --- Executes on button press in exgraf2.
function exgraf2_Callback(hObject, eventdata, handles)
specgraf=(get(handles.exgraf2,'Value'))


%--------------------------------------------------------------------------
%                 A B O U T
%--------------------------------------------------------------------------
% --------------------------------------------------------------------
function Aboat_Callback(hObject, eventdata, handles)
zprava{1,1}=(' Název aplikace : Úprava a klasifikace EKG signálů');
zprava{2,1}=('                 Verze : 1.0');
zprava{3,1}=('Datum vytvoření : 25.5.2008');
zprava{4,1}=('                   Účel : Bakalářská práce');
zprava{6,1}=('                  Autor : David Loviška');
zprava{7,1}=('   Vedoucí práce : Ing.Jan Hrubeš');
msgbox(zprava,'Tip','warn')


%--------------------------------------------------------------------------
%                 H E L P
%--------------------------------------------------------------------------
% --------------------------------------------------------------------
function navod_Callback(hObject, eventdata, handles)
zprava{1,1}=('Aplikace využívá externí výpočty.');
zprava{2,1}=('Proto je nutné postupovat následovně :');
zprava{4,1}=('1) Pomocí tlačítka Browse na horní liště,nebo uvnitř aplikace ');
zprava{5,1}=('    vybrat textový soubor se vstupními daty.');
zprava{6,1}=('    Musí se jednat o soubor *.TXT');
zprava{7,1}=('2) Nyní je nutné provést úpravy a filtrace vstupních dat.');
zprava{8,1}=('    Před tím lze zvolit,zda se má zobrazit průběh filtrovaného signálu a ');
zprava{9,1}=('    jeho spektrum včetně výsledného signálu.');
zprava{10,1}=('    To se provádí zaškrtnutím zatržítek.');
zprava{11,1}=('    Po nastavení provedeme úpravy stiskem tlačítka "Make EKG file".');
zprava{12,1}=('3) Samotnou klasifikaci provedeme tlačítkem "R U N".');
zprava{13,1}=('4) V okně "Výsledky" se objeví časy R-vln.');
zprava{14,1}=('    Poklepáním na čas R-vlny se nám daná vlna v grafu zvýrazní.');
zprava{15,1}=('5) Na spodním grafu je celý průběh a posunem posuvníku se nám ');
zprava{16,1}=('    v horním grafu zobrazí detail.');

msgbox(zprava,'Tip','help')


%--------------------------------------------------------------------------
%                 RR - interval - indikace
%--------------------------------------------------------------------------



% --- Executes on selection change in RRinterval.
function RRinterval_Callback(hObject, eventdata, handles)
Signal=get(handles.Signal,'string');
timx=get(handles.timx,'string');
puk=get(handles.puk,'string');
Rvlnbod = (get(hObject,'Value'))
indik=str2double(puk(Rvlnbod,:))
indik1=str2double(puk(Rvlnbod+1,:))
    pomoccas=timx(indik,:);
    pomoccas1=timx(indik1,:);
    pomocsig=max(str2num(Signal(indik-5:indik+5,:)))
    pomocsig1=max(str2num(Signal(indik1-5:indik1+5,:)))
axes(handles.graflupa)
plot(str2num(pomoccas),(pomocsig),'r.', 'MarkerSize',30);
plot(str2num(pomoccas1),(pomocsig1),'r.', 'MarkerSize',30);
    hold on;
axes(handles.grafcelk)
plot(str2num(pomoccas),(pomocsig),'r.', 'MarkerSize',20);
plot(str2num(pomoccas1),(pomocsig1),'r.', 'MarkerSize',20);
    hold on;


% --- Executes during object creation, after setting all properties.
function RRinterval_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%--------------------------------------------------------------------------
%                 Problemy - indikace
%--------------------------------------------------------------------------

% --- Executes on selection change in problemy.
function problemy_Callback(hObject, eventdata, handles)


% --- Executes during object creation, after setting all properties.
function problemy_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




%--------------------------------------------------------------------------
%                 T a S vlny - indikace
%--------------------------------------------------------------------------


% --- Executes on selection change in TS.
function TS_Callback(hObject, eventdata, handles)


% --- Executes during object creation, after setting all properties.
function TS_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


%--------------------------------------------------------------------------
%                 Výstup umělé neuronové sítě
%--------------------------------------------------------------------------

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

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


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

% Hint: listbox 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


