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

% Last Modified by GUIDE v2.5 26-Apr-2019 22:50:05

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

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

set(handles.Gui1,'Units','normalized','Position',[0 0 1 1],'Name','Kinematická analýza');
set(handles.Tbn_plot,'Enable','off');set(handles.axes1,'visible','off');
set(handles.all,'Enable','off');set(handles.table,'visible','off');
set(handles.text_t1,'visible','off');set(handles.text_t2,'visible','off');set(handles.text_t3,'visible','off');
set(handles.dalsi_graf,'Visible','off');set(handles.predchozi_graf,'Visible','off')
set(handles.extremy,'Visible','off');set(handles.nadpis_extremy,'visible','off'); set(handles.Load_text,'string', 'Načtěte data pro zobrazení.');
 
set(handles.p1,'Enable','off');set(handles.p2,'Enable','off');set(handles.p3,'Enable','off');
set(handles.h1,'Enable','off');set(handles.h2,'Enable','off');set(handles.h3,'Enable','off');
set(handles.k1,'Enable','off');set(handles.k2,'Enable','off');set(handles.k3,'Enable','off');
set(handles.a1,'Enable','off');set(handles.a2,'Enable','off');set(handles.a3,'Enable','off');

set(handles.p1,'Value',0);set(handles.p2,'Value',0);set(handles.p3,'Value',0);
set(handles.h1,'Value',0);set(handles.h2,'Value',0);set(handles.h3,'Value',0);
set(handles.k1,'Value',0);set(handles.k2,'Value',0);set(handles.k3,'Value',0);
set(handles.a1,'Value',0);set(handles.a2,'Value',0);set(handles.a3,'Value',0);set(handles.all,'Value',0);
%% načtení normy
handles.norma_file=load('norma.mat');

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes GUI_1 wait for user response (see UIRESUME)
% uiwait(handles.Gui1);


% --- Outputs from this function are returned to the command line.
function varargout = GUI_1_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 Tbn_plot.
function Tbn_plot_Callback(hObject, eventdata, handles)
% hObject    handle to Tbn_plot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global indexy;global var;
tituls={'Hip Flexion/Extension';'Hip Ab/Adduction';'Hip Rotation';'Knee Flexion/Extension';'Knee Ab/Adduction';'Knee Rotation';'Ankle Dorsi/Plantar';'Ankle Ab/Adduction';'Foot Progress Angles';'Pelvic Tilt';'Pelvic Obliquity';'Pelvic Rotation'};
    
    if numel(indexy)==0
        set(handles.text_t1,'visible','off');set(handles.text_t2,'visible','off');set(handles.text_t3,'visible','off');
        set(handles.dalsi_graf,'Visible','off');set(handles.predchozi_graf,'Visible','off')
        set(handles.extremy,'Visible','off');set(handles.nadpis_extremy,'visible','off');
        cla(handles.axes1,'reset');set(handles.axes1,'Visible','off');

        uiwait(msgbox('Nebyla vybrána veličina k zobrazení','modal'));
    else
        var=1;
        set(handles.dalsi_graf,'Visible','on');set(handles.predchozi_graf,'Visible','on')
        set(handles.predchozi_graf,'Enable','off');set(handles.dalsi_graf,'Enable','on');
        n_data=struct2cell(handles.norma_file);
        n_data=n_data{1,1};
        p_data=struct2cell(handles.mat_file);
        p_data=p_data{1,1};

        variable_n=n_data{indexy(var),1};
        mean=variable_n(1,:);
        std=variable_n(2,:);
        x=1:size(variable_n,2);

        set(handles.axes1,'visible','on');
        cla(handles.axes1,'reset');
        axes(handles.axes1);
        patch([x fliplr(x)],[mean-(2*std) fliplr(mean+(2*std))],[0.9 0.9 0.9]);hold on;
        plot(p_data{indexy(var),1},'b','LineWidth',1.5);plot(p_data{indexy(var),2},'r','LineWidth',1.5);
        xlabel('% krokového cyklu','FontSize',12);ylabel('Úhel [°]','FontSize',12);
        title(tituls{indexy(var),1},'FontSize',16);
        legend('Norma','Levá noha','Pravá noha','Location','best','Orientation','horizontal');
        set(gca,'YGrid','on','Layer','top');   %'YMinorGrid','on'
               
        %% TABULKA
        d=cell(3,2);
        set(handles.table,'Data',cell(3,2));
         if ismember(1,indexy) || ismember(4,indexy) || ismember(7,indexy) 
            set(handles.table,'visible','on');
            set(handles.text_t1,'visible','on');set(handles.text_t2,'visible','on');set(handles.text_t3,'visible','on');
            set(handles.extremy,'Visible','on');set(handles.nadpis_extremy,'visible','on');
            if ismember(1,indexy)
                pom_L=p_data{1,1};pom_R=p_data{1,2};peak=1;
                [c_L,c_L_ind,c_R,c_R_ind]=segments(pom_L,pom_R,peak);
                d{1,1}=c_L;d{1,2}=c_R;
            end
            if ismember(4,indexy)
               pom_L=p_data{4,1};pom_R=p_data{4,2};peak=1;
               [c_L,c_L_ind,c_R,c_R_ind]=segments(pom_L,pom_R,peak);
                d{2,1}=c_L;d{2,2}=c_R;
            end
            if ismember(7,indexy)
               pom_L=p_data{7,1};pom_R=p_data{7,2};peak=2;
               [c_L,c_L_ind,c_R,c_R_ind]=segments(pom_L,pom_R,peak);
               d{3,1}=c_L; d{3,2}=c_R;
            end

            if ismember(1,indexy) == false
               d{1,1}='      -'; d{1,2}='      -';
            end
            if ismember(4,indexy) == false
               d{2,1}='      -'; d{2,2}='      -';
            end
            if ismember(7,indexy) == false
               d{3,1}='      -'; d{3,2}='      -';
            end

            set(handles.table,'Data',d); 
            
         else
             set(handles.table,'visible','off');set(handles.text_t1,'visible','off');
             set(handles.text_t2,'visible','off');set(handles.text_t3,'visible','off');
             set(handles.extremy,'Visible','off');set(handles.nadpis_extremy,'visible','off'); 
         end
         if length(indexy)==12;
            norma=load('norma.mat');
            p_data=handles.mat_file;

            setappdata(handles.Gui1,'index',indexy);
            setappdata(handles.Gui1,'norma',norma);
            setappdata(handles.Gui1,'pacient',p_data);

            GUI_2_all();
         end 
    end

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

 set(handles.Load_text,'string', 'Načtěte data pro zobrazení.');
 set(handles.all,'Enable','off');
 set(handles.p1,'Enable','off');set(handles.p2,'Enable','off');set(handles.p3,'Enable','off');
 set(handles.h1,'Enable','off');set(handles.h2,'Enable','off');set(handles.h3,'Enable','off');
 set(handles.k1,'Enable','off');set(handles.k2,'Enable','off');set(handles.k3,'Enable','off');
 set(handles.a1,'Enable','off');set(handles.a2,'Enable','off');set(handles.a3,'Enable','off');
 
 set(handles.all,'Value',0);
 set(handles.p1,'Value',0);set(handles.p2,'Value',0);set(handles.p3,'Value',0);
 set(handles.h1,'Value',0);set(handles.h2,'Value',0);set(handles.h3,'Value',0);
 set(handles.k1,'Value',0);set(handles.k2,'Value',0);set(handles.k3,'Value',0);
 set(handles.a1,'Value',0);set(handles.a2,'Value',0);set(handles.a3,'Value',0);
 
 set(handles.Tbn_plot,'Enable','off');cla(handles.axes1,'reset');
 set(handles.axes1,'Visible','off');set(handles.table,'visible','off');
 set(handles.text_t1,'visible','off');set(handles.text_t2,'visible','off');set(handles.text_t3,'visible','off');
 set(handles.dalsi_graf,'Visible','off');set(handles.predchozi_graf,'Visible','off');
 set(handles.extremy,'Visible','off');set(handles.nadpis_extremy,'visible','off');
 
%% načtení dat
FileName = uigetfile('*.mat','Vyberte soubor *.mat pro zobrazení.');
if(~isequal(FileName,0))
   handles.mat_file=load(FileName);
    data_kontrola=struct2cell(handles.mat_file);
    data_kontrola=data_kontrola{1,1};
   [a b]=size(data_kontrola);
    if a==12 && b==2
       set(handles.Load_text,'string', 'Data úspěšně načtena. Zvolte analyzované veličiny.');      % informace 
       
       % Enable tlačítko ZOBRAZIT, veliciny
         set(handles.Tbn_plot,'Enable','on');set(handles.all,'Enable','on');
         set(handles.p1,'Enable','on');set(handles.p2,'Enable','on');set(handles.p3,'Enable','on');
         set(handles.h1,'Enable','on');set(handles.h2,'Enable','on');set(handles.h3,'Enable','on');
        set(handles.k1,'Enable','on');set(handles.k2,'Enable','on');set(handles.k3,'Enable','on');
        set(handles.a1,'Enable','on');set(handles.a2,'Enable','on');set(handles.a3,'Enable','on');
         global indexy;indexy=[];
    else 
        uiwait(msgbox('Špatná data','modal'));
%         uiwait(handles.Gui1);
    end
   
   
end

%% Update data 
    guidata(hObject, handles);  

% --- Executes on button press in k3.
function k3_Callback(hObject, eventdata, handles)
% hObject    handle to k3 (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 k3
global indexy;
if (get(hObject,'Value') == get(hObject,'Max'));
    indexy=[indexy 6];
elseif (get(hObject,'Value') ~= get(hObject,'Max'));
    ind=find(indexy==6);
    if isempty(ind) == 0;
       indexy(ind)=[];
    end
end

% --- Executes on button press in k2.
function k2_Callback(hObject, eventdata, handles)
% hObject    handle to k2 (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 k2
global indexy;
if (get(hObject,'Value') == get(hObject,'Max'));
    indexy=[indexy 5];
elseif (get(hObject,'Value') ~= get(hObject,'Max'));
    ind=find(indexy==5);
    if isempty(ind) == 0;
       indexy(ind)=[];
    end
end

% --- Executes on button press in k1.
function k1_Callback(hObject, eventdata, handles)
% hObject    handle to k1 (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 k1
global indexy;
if (get(hObject,'Value') == get(hObject,'Max'));
    indexy=[indexy 4];
elseif (get(hObject,'Value') ~= get(hObject,'Max'));
    ind=find(indexy==4);
    if isempty(ind) == 0;
       indexy(ind)=[];
    end
end

% --- Executes on button press in a3.
function a3_Callback(hObject, eventdata, handles)
% hObject    handle to a3 (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 a3
global indexy;
if (get(hObject,'Value') == get(hObject,'Max'));
    indexy=[indexy 9];
elseif (get(hObject,'Value') ~= get(hObject,'Max'));
    ind=find(indexy==9);
    if isempty(ind) == 0;
       indexy(ind)=[];
    end
end

% --- Executes on button press in a2.
function a2_Callback(hObject, eventdata, handles)
% hObject    handle to a2 (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 a2
global indexy;
if (get(hObject,'Value') == get(hObject,'Max'));
    indexy=[indexy 8];
elseif (get(hObject,'Value') ~= get(hObject,'Max'));
    ind=find(indexy==8);
    if isempty(ind) == 0;
       indexy(ind)=[];
    end
end

% --- Executes on button press in a1.
function a1_Callback(hObject, eventdata, handles)
% hObject    handle to a1 (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 a1
global indexy;
if (get(hObject,'Value') == get(hObject,'Max'));
    indexy=[indexy 7];
elseif (get(hObject,'Value') ~= get(hObject,'Max'));
    ind=find(indexy==7);
    if isempty(ind) == 0;
       indexy(ind)=[];
    end
end

% --- Executes on button press in h3.
function h3_Callback(hObject, eventdata, handles)
% hObject    handle to h3 (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 h3
global indexy;
if (get(hObject,'Value') == get(hObject,'Max'));
    indexy=[indexy 3];
elseif (get(hObject,'Value') ~= get(hObject,'Max'));
    ind=find(indexy==3);
    if isempty(ind) == 0;
       indexy(ind)=[];
    end
end

% --- Executes on button press in h2.
function h2_Callback(hObject, eventdata, handles)
% hObject    handle to h2 (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 h2
global indexy;
if (get(hObject,'Value') == get(hObject,'Max'));
    indexy=[indexy 2];
elseif (get(hObject,'Value') ~= get(hObject,'Max'));
    ind=find(indexy==2);
    if isempty(ind) == 0;
       indexy(ind)=[];
    end
end

% --- Executes on button press in h1.
function h1_Callback(hObject, eventdata, handles)
% hObject    handle to h1 (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 h1
global indexy;
if (get(hObject,'Value') == get(hObject,'Max'));
    indexy=[indexy 1];
elseif (get(hObject,'Value') ~= get(hObject,'Max'));
    ind=find(indexy==1);
    if isempty(ind) == 0;
       indexy(ind)=[];
    end
end

% --- Executes on button press in p3.
function p3_Callback(hObject, eventdata, handles)
% hObject    handle to p3 (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 p3
global indexy;
if (get(hObject,'Value') == get(hObject,'Max'));
    indexy=[indexy 12];
elseif (get(hObject,'Value') ~= get(hObject,'Max'));
    ind=find(indexy==12);
    if isempty(ind) == 0;
       indexy(ind)=[];
    end
end

% --- Executes on button press in p2.
function p2_Callback(hObject, eventdata, handles)
% hObject    handle to p2 (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 p2
global indexy;
if (get(hObject,'Value') == get(hObject,'Max'));
    indexy=[indexy 11];
elseif (get(hObject,'Value') ~= get(hObject,'Max'));
    ind=find(indexy==11);
    if isempty(ind) == 0;
       indexy(ind)=[];
    end
end

% --- Executes on button press in p1.
function p1_Callback(hObject, eventdata, handles)
% hObject    handle to p1 (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 p1
global indexy;
if (get(hObject,'Value') == get(hObject,'Max'));
    indexy=[indexy 10];
elseif (get(hObject,'Value') ~= get(hObject,'Max'));
    ind=find(indexy==10);
    if isempty(ind) == 0;
       indexy(ind)=[];
    end
end

% --- Executes on button press in dalsi_graf.
function dalsi_graf_Callback(hObject, eventdata, handles)
% hObject    handle to dalsi_graf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global indexy;global var;
tituls={'Hip Flexion/Extension';'Hip Ab/Adduction';'Hip Rotation';'Knee Flexion/Extension';'Knee Ab/Adduction';'Knee Rotation';'Ankle Dorsi/Plantar';'Ankle Ab/Adduction';'Foot Progress Angles';'Pelvic Tilt';'Pelvic Obliquity';'Pelvic Rotation'};

var=var+1;
set(handles.predchozi_graf,'Enable','on');
if var>length(indexy)
    var=length(indexy);
    set(handles.dalsi_graf,'Enable','off');
    set(handles.predchozi_graf,'Enable','on');
else    
    n_data=struct2cell(handles.norma_file);
    n_data=n_data{1,1};
    p_data=struct2cell(handles.mat_file);
    p_data=p_data{1,1};

    variable_n1=n_data{indexy(var),1};
    mean=variable_n1(1,:);
    std=variable_n1(2,:);
    x=1:size(variable_n1,2);

    cla(handles.axes1,'reset');
    axes(handles.axes1);
    patch([x fliplr(x)],[mean-(2*std) fliplr(mean+(2*std))],[0.9 0.9 0.9]);hold on;
    plot(p_data{indexy(var),1},'b','LineWidth',1.5);plot(p_data{indexy(var),2},'r','LineWidth',1.5);
    xlabel('% krokového cyklu','FontSize',12);ylabel('Úhel [°]','FontSize',12);
    title(tituls{indexy(var),1},'FontSize',16);
    legend('Norma','Levá noha','Pravá noha','Location','best','Orientation','horizontal');
    set(gca,'YGrid','on','Layer','top');    %'YMinorGrid','on',
 end

% --- Executes on button press in predchozi_graf.
function predchozi_graf_Callback(hObject, eventdata, handles)
% hObject    handle to predchozi_graf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global indexy;global var;
tituls={'Hip Flexion/Extension';'Hip Ab/Adduction';'Hip Rotation';'Knee Flexion/Extension';'Knee Ab/Adduction';'Knee Rotation';'Ankle Dorsi/Plantar';'Ankle Ab/Adduction';'Foot Progress Angles';'Pelvic Tilt';'Pelvic Obliquity';'Pelvic Rotation'};

var=var-1;
if var<1
    var=1;
    set(handles.predchozi_graf,'Enable','off');
    set(handles.dalsi_graf,'Enable','on');
else
    set(handles.dalsi_graf,'Enable','on');   
    n_data=struct2cell(handles.norma_file);
    n_data=n_data{1,1};
    p_data=struct2cell(handles.mat_file);
    p_data=p_data{1,1};

    variable_n1=n_data{indexy(var),1};
    mean=variable_n1(1,:);
    std=variable_n1(2,:);
    x=1:size(variable_n1,2);

    cla(handles.axes1,'reset');
    axes(handles.axes1);
    patch([x fliplr(x)],[mean-(2*std) fliplr(mean+(2*std))],[0.9 0.9 0.9]);hold on;
    plot(p_data{indexy(var),1},'b','LineWidth',1.5);plot(p_data{indexy(var),2},'r','LineWidth',1.5);
    xlabel('% krokového cyklu','FontSize',12);ylabel('Úhel [°]','FontSize',12);
    title(tituls{indexy(var),1},'FontSize',16);
    legend('Norma','Levá noha','Pravá noha','Location','best','Orientation','horizontal');
    set(gca,'YGrid','on','Layer','top');   %'YMinorGrid','on'
end

% --- Executes on button press in all.
function all_Callback(hObject, eventdata, handles)
% hObject    handle to all (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 all
global indexy;

if (get(hObject,'Value') == get(hObject,'Max'));
       
    set(handles.p1,'Value',1);set(handles.p2,'Value',1);set(handles.p3,'Value',1);
    set(handles.h1,'Value',1);set(handles.h2,'Value',1);set(handles.h3,'Value',1);
    set(handles.k1,'Value',1);set(handles.k2,'Value',1);set(handles.k3,'Value',1);
    set(handles.a1,'Value',1);set(handles.a2,'Value',1);set(handles.a3,'Value',1);
    indexy=[1 2 3 4 5 6 7 8 9 10 11 12];
       
elseif (get(hObject,'Value') ~= get(hObject,'Max'));
    
    set(handles.p1,'Value',0);set(handles.p2,'Value',0);set(handles.p3,'Value',0);
    set(handles.h1,'Value',0);set(handles.h2,'Value',0);set(handles.h3,'Value',0);
    set(handles.k1,'Value',0);set(handles.k2,'Value',0);set(handles.k3,'Value',0);
    set(handles.a1,'Value',0);set(handles.a2,'Value',0);set(handles.a3,'Value',0);
    indexy=[];
    
end
