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

% Last Modified by GUIDE v2.5 28-Apr-2015 15:16:19

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = HistoEmoGUI_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;
function experiment_Callback(hObject, eventdata, handles)
% hObject    handle to experiment (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 experiment contents as cell array
%        contents{get(hObject,'Value')} returns selected item from experiment
experiment=get(hObject,'String');
experiment_index=get(hObject,'value');
handles.experiment=experiment{experiment_index};
guidata(hObject, handles)


% --- Executes during object creation, after setting all properties.
function experiment_CreateFcn(hObject, eventdata, handles)
% hObject    handle to experiment (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
handles.experiments = {'EFED1', 'EFED2', 'EFE1', 'EFE2'};
set(hObject, 'String',  handles.experiments);


function min_Callback(hObject, eventdata, handles)
% hObject    handle to min (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 min as text
%        str2double(get(hObject,'String')) returns contents of min as a double
handles.box1=get(hObject, 'String');
guidata(hObject, handles)

% --- Executes during object creation, after setting all properties.
function min_CreateFcn(hObject, eventdata, handles)
% hObject    handle to min (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 max_Callback(hObject, eventdata, handles)
% hObject    handle to max (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 max as text
%        str2double(get(hObject,'String')) returns contents of max as a double
handles.box2=get(hObject, 'String');
guidata(hObject, handles)

% --- Executes during object creation, after setting all properties.
function max_CreateFcn(hObject, eventdata, handles)
% hObject    handle to max (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 emotions.
function emotions_Callback(hObject, eventdata, handles)
% hObject    handle to emotions (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
emotion=get(hObject,'String');
emotion_index=get(hObject,'value');
handles.emotion=emotion{emotion_index};
guidata(hObject, handles)
% Hints: contents = cellstr(get(hObject,'String')) returns emotions contents as cell array
%        contents{get(hObject,'Value')} returns selected item from emotions


% --- Executes during object creation, after setting all properties.
function emotions_CreateFcn(hObject, eventdata, handles)
% hObject    handle to emotions (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
handles.Emotions = {'All','Neutral', 'Disgust', 'Surprise', 'Happiness', 'Fear'};
set(hObject, 'String',  handles.Emotions);



function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (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 edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double
handles.box3=get(hObject, 'String');
guidata(hObject, handles)

% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (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 edit4_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (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 edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double
handles.box4=get(hObject, 'String');
guidata(hObject, handles)

% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (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 ROI.
function ROI_Callback(hObject, eventdata, handles)
% hObject    handle to ROI (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 ROI contents as cell array
%        contents{get(hObject,'Value')} returns selected item from ROI
roi=get(hObject,'String');
roi_index=get(hObject,'value');
handles.roi=roi{roi_index};
guidata(hObject, handles)


% --- Executes during object creation, after setting all properties.
function ROI_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ROI (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
handles.roi={'mouth','eyes','nose','corr','front','chin','Brows'};
set(hObject, 'String', handles.roi)


% --- Executes on button press in percbox.
function percbox_Callback(hObject, eventdata, handles)
% hObject    handle to percbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.percbox=get(hObject,'Value')
% Hint: get(hObject,'Value') returns toggle state of percbox
guidata(hObject, handles)

% --- Executes on button press in fixperemo.
function fixperemo_Callback(hObject, eventdata, handles)
% hObject    handle to fixperemo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.fixperemo=get(hObject,'Value');
% Hint: get(hObject,'Value') returns toggle state of fixperemo
guidata(hObject, handles)


% --- Executes on button press in plotemotion.
function plotemotion_Callback(hObject, eventdata, handles)
% hObject    handle to plotemotion (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if strcmp(handles.experiment, 'EFED1') || strcmp(handles.experiment, 'EFED2')
    path=get(handles.textpath, 'String')
    path=sprintf('%s\\EventFixDynamic.mat', path)
    limitdwn=str2num(get(handles.limitdwn, 'String'))
    limitup=str2num(get(handles.limitup, 'String'))
    figure(1);
    set(figure(1),'Units','Normalized','OuterPosition',[0 0 1 1])
    subplot(2,2,1) %general
    a=1;
    b=5000;
    [statistics]=HistoEmotions (path, handles.emotion, a, b, handles.experiment, limitdwn, limitup)
    set(handles.general,'String',statistics)
    subplot(2,2,2) %preapex
    mini1=str2num(get(handles.min, 'String'));
    maxi1=str2num(get(handles.max, 'String'));
    [statistics]=HistoEmotions (path,handles.emotion, mini1, maxi1, handles.experiment, limitdwn, limitup)
    set(handles.interval1,'String',statistics)
    subplot(2,2,3) %apex
    c=2400;
    d=2600;
    [statistics]=HistoEmotions (path,handles.emotion, c, d, handles.experiment, limitdwn, limitup)
    set(handles.apex,'String',statistics)
    subplot(2,2,4) %afterapex
    mini2=str2num(get(handles.edit3, 'String'));
    maxi2=str2num(get(handles.edit4, 'String'));
    [statistics]=HistoEmotions (path,handles.emotion, mini2, maxi2, handles.experiment, limitdwn, limitup)
    set(handles.interval2,'String',statistics)
    
else
    path=get(handles.textpath, 'String')
    path=sprintf('%s\\EventFixStatic.mat', path)
    limitdwn=str2num(get(handles.limitdwn, 'String'))
    limitup=str2num(get(handles.limitup, 'String'))
    figure(1);
    set(figure(1),'Units','Normalized','OuterPosition',[0 0 1 1])
    [statistics]=HistoEmotions (path,handles.emotion, 0, 5000, handles.experiment, limitdwn, limitup)
    set(handles.general,'String',statistics)
    set(handles.interval1,'String',[])
    set(handles.interval2,'String',[])
    set(handles.apex,'String',[])
end
guidata(hObject, handles)

% --- Executes on button press in plotall.
function plotall_Callback(hObject, eventdata, handles)
% hObject    handle to plotall (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
check1 = handles.fixperemo
check2 = handles.percbox
export=0;
if check1==1
    checks=1; 
elseif check2==1
    checks=0;
end
if strcmp(handles.experiment, 'EFED1') || strcmp(handles.experiment, 'EFED2')
    path=get(handles.textpath, 'String')
    path=sprintf('%s\\EventFixDynamic.mat', path)
    limitdwn=str2num(get(handles.limitdwn, 'String'))
    limitup=str2num(get(handles.limitup, 'String'))
    figure(1)
    subplot(2,2,1) %general
    a=1;
    b=5000;
    AllEmotions(handles.experiment, a, b, checks, path, limitup, limitdwn,export)
    subplot(2,2,2) %preapex
    mini1=str2num(get(handles.min, 'String'));
    maxi1=str2num(get(handles.max, 'String'));
    AllEmotions(handles.experiment,mini1, maxi1, checks, path, limitup, limitdwn, export)
    subplot(2,2,3) %apex
    c=2400;
    d=2600;
    AllEmotions(handles.experiment,c, d, checks, path, limitup, limitdwn, export)
    subplot(2,2,4) %afterapex
    mini2=str2num(get(handles.edit3, 'String'));
    maxi2=str2num(get(handles.edit4, 'String'));
    AllEmotions(handles.experiment,mini2, maxi2, checks, path, limitup, limitdwn, export)
    set(handles.general,'String',[])
    set(handles.interval1,'String',[])
    set(handles.interval2,'String',[])
    set(handles.apex,'String',[])
else
    path=get(handles.textpath, 'String')
    path=sprintf('%s\\EventFixStatic.mat', path)
    limitdwn=str2num(get(handles.limitdwn, 'String'))
    limitup=str2num(get(handles.limitup, 'String'))
    figure(1);
    AllEmotions(handles.experiment,0, 5000, checks, path, limitup, limitdwn, export)
    set(handles.general,'String',[])
    set(handles.interval1,'String',[])
    set(handles.interval2,'String',[])
    set(handles.apex,'String',[])
end
guidata(hObject, handles)



function limitdwn_Callback(hObject, eventdata, handles)
% hObject    handle to limitdwn (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 limitdwn as text
%        str2double(get(hObject,'String')) returns contents of limitdwn as a double
handles.box5=get(hObject, 'String');
guidata(hObject, handles)


% --- Executes during object creation, after setting all properties.
function limitdwn_CreateFcn(hObject, eventdata, handles)
% hObject    handle to limitdwn (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 limitup_Callback(hObject, eventdata, handles)
% hObject    handle to limitup (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 limitup as text
%        str2double(get(hObject,'String')) returns contents of limitup as a double
handles.box6=get(hObject, 'String');
guidata(hObject, handles)


% --- Executes during object creation, after setting all properties.
function limitup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to limitup (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 exportPICS.
function exportPICS_Callback(hObject, eventdata, handles)
% hObject    handle to exportPICS (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
limitdwn=str2num(get(handles.limitdwn, 'String'))
limitup=str2num(get(handles.limitup, 'String'))
way=get(handles.textpath2, 'String');
filename=sprintf('\\graph%s_%s_%d_%dms.bmp', handles.experiment, handles.emotion, limitdwn, limitup);
filename=strcat(way, filename)
figure(1)
saveas(gcf, filename)
guidata(hObject, handles)

% --- Executes on button press in durationbutton.HistoEmoGUI
function durationbutton_Callback(hObject, eventdata, handles)
% hObject    handle to durationbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if strcmp(handles.experiment, 'EFED1') || strcmp(handles.experiment, 'EFED2')
    path=get(handles.textpath, 'String')
    path=sprintf('%s\\EventFixDynamic.mat', path)
else
    path=get(handles.textpath, 'String')
    path=sprintf('%s\\EventFixStatic.mat', path)
end
 limitdwn=str2num(get(handles.limitdwn, 'String'))
 limitup=str2num(get(handles.limitup, 'String'))
 way=get(handles.textpath2, 'String')
 XLSexportDuration(path, handles.experiment, way, limitdwn, limitup)


% --- Executes on button press in fixationbutton.
function fixationbutton_Callback(hObject, eventdata, handles)
% hObject    handle to fixationbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
export=1;
way=get(handles.textpath2, 'String')
if strcmp(handles.experiment, 'EFED1') || strcmp(handles.experiment, 'EFED2')
    path=get(handles.textpath, 'String')
    path=sprintf('%s\\EventFixDynamic.mat', path)
else
    path=get(handles.textpath, 'String')
    path=sprintf('%s\\EventFixStatic.mat', path)
end
mini=0;
maxi=5000;
limitdwn=str2num(get(handles.limitdwn, 'String'))
limitup=str2num(get(handles.limitup, 'String'))
check=2;
AllEmotions(handles.experiment,mini, maxi, check, path, limitup, limitdwn, export,way)
guidata(hObject, handles)


% --- Executes on button press in path.
function path_Callback(hObject, eventdata, handles)
% hObject    handle to path (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
folder_name=uigetdir;
set(handles.textpath, 'String', folder_name)
guidata(hObject, handles)


% --- Executes on button press in expath.
function expath_Callback(hObject, eventdata, handles)
% hObject    handle to expath (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
folder_name2=uigetdir;
set(handles.textpath2, 'String', folder_name2)
guidata(hObject, handles)


% --- Executes on button press in CR.
function CR_Callback(hObject, eventdata, handles)
% hObject    handle to CR (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if strcmp(handles.experiment, 'EFED2')
    path=get(handles.textpath, 'String')
    path=sprintf('%s\\EventFixDynamic.mat', path)
    figure(1)
    correctratio(path, handles.experiment)
elseif strcmp(handles.experiment, 'EFE2')
    path=get(handles.textpath, 'String')
    path=sprintf('%s\\EventFixStatic.mat', path)
    figure(1)
    correctratio(path, handles.experiment)
else
    warndlg('Choose only EFE2/EFED2')
end
guidata(hObject, handles)


% --- Executes on button press in FixInVid.
function FixInVid_Callback(hObject, eventdata, handles)
% hObject    handle to FixInVid (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
limitdwn=str2num(get(handles.limitdwn, 'String'))
limitup=str2num(get(handles.limitup, 'String'))
if strcmp(handles.experiment, 'EFED1') || strcmp(handles.experiment, 'EFED2')
    path=get(handles.textpath, 'String')
    path=sprintf('%s\\EventFixDynamic.mat', path)
else
    path=get(handles.textpath, 'String')
    path=sprintf('%s\\EventFixStatic.mat', path)
end
FixInVid(path, handles.experiment, 1, 5000, limitup, limitdwn)
guidata(hObject, handles)