function varargout = ExportDataExcel(varargin)
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
%     G U I   H E L P   I N F O R M A T I O N                                   % 
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
% ExportDataExcel m-file for ExportDataExcel.fig                                % 
%                                                                               % 
%       The GUI is part of the LFP Analysis Package 2006 and is to be           %     
%       Launched from the Fron_Panel Window of the Package                      %
%                                                                               % 
%       The software is designed to provide the user with the ability to        %
%       obtain accurate information about the shape and size of a LFP
%       through exporting cursor points to Excel.
%                                                                               % 
%       After clicking the Activate cursor button, clicking on the graph
%       will show x and y coordinates. upon clicking on th ebutton again,
%       this utility will stop, however the cursor must be clicked once         %
%       more to clear the cross-hair. 
%                                                                               % 
%       Top export points to excel, enter a filepath and file name              %
%       (including the .xls extension) in the edit box and click the            %
%       'Export coords to Excel' button. Again, upone de selecting, the
%       cursor must be clicked once more to remove the crosshair.
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 

%  This Software is distributed under the GNU General Public Liscence           % 
%  a copy of this liscence sould be accompany this software.                    % 
%                                                                               % 
% Copyright (C) 2006  Richard Peter and Greg Aldam                              %
%                                                                               %  
% This program is free software; you can redistribute it and/or modify          %
% it under the terms of the GNU General Public License as published by          %
% the Free Software Foundation; either version 2 of the License, or             %
% (at your option) any later version.
%                                                                               % 
% This program is distributed in the hope that it will be useful,               %
% but WITHOUT ANY WARRANTY; without even the implied warranty of                %
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 %
% GNU General Public License for more details.                                  %
%                                                                               %  
% You should have received a copy of the GNU General Public License             %
% along with this program; if not, write to the Free Software                   %
% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA %
%                                                                               % 
%                                                                               % 
%  For further information contact Richard Peter at                             % 
%  infinitybeckon@hotmail.com                                                   % 
%                                                                               %   
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % 

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

% Choose default command line output for ExportDataExcel
handles.output = hObject;
handles.h = varargin{1};
% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = ExportDataExcel_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 FPath_Callback(hObject, eventdata, handles)
% hObject    handle to FPath (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 FPath as text
%        str2double(get(hObject,'String')) returns contents of FPath as a double


% --- Executes during object creation, after setting all properties.
function FPath_CreateFcn(hObject, eventdata, handles)
% hObject    handle to FPath (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 WSheet_Callback(hObject, eventdata, handles)
% hObject    handle to WSheet (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 WSheet as text
%        str2double(get(hObject,'String')) returns contents of WSheet as a double


% --- Executes during object creation, after setting all properties.
function WSheet_CreateFcn(hObject, eventdata, handles)
% hObject    handle to WSheet (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 GoBtn.
function GoBtn_Callback(hObject, eventdata, handles)
% hObject    handle to GoBtn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if length(get(handles.FPath, 'String'))>=5 %they must append .xls
    if ~length(get(handles.WSheet, 'String'))>=1
        set(handles.WSheet, 'String', 'Power Data');
    else
        %Make a mask of the requird data
        Db = [get(handles.h.DeltaSel, 'Value'), get(handles.h.ThetaSel, 'Value'), ...
            get(handles.h.AlphSel, 'Value'), get(handles.h.BetSel, 'Value'), get(handles.h.GamLSel, 'Value')...
            get(handles.h.GamHSel, 'Value'), get(handles.h.GamVHSel, 'Value'), get(handles.h.Cust1Sel, 'Value')...
            get(handles.h.Cust2Sel, 'Value'), get(handles.h.Cust3Sel, 'Value')];

        GetThese= find(Db ==1);
        Pows = [str2num(get(handles.h.DeltaPow, 'String')),   str2num(get(handles.h.ThetaPow, 'String')), ...
             str2num(get(handles.h.AlphaPow, 'String')),   str2num(get(handles.h.BetaPow, 'String')),   str2num(get(handles.h.GamLPow, 'String')),...
             str2num(get(handles.h.GamHPow, 'String')),   str2num(get(handles.h.GamVHPow, 'String')),   str2num(get(handles.h.Cust1Pow, 'String')),...
             str2num(get(handles.h.Cust2Pow, 'String')),   str2num(get(handles.h.Cust3Pow, 'String'))];
        PcPows = [ str2num(get(handles.h.DeltaPcPow, 'String')),  str2num(get(handles.h.ThetaPcPow, 'String')), ...
             str2num(get(handles.h.AlphaPcPow, 'String'))   str2num(get(handles.h.BetaPcPow, 'String'))   str2num(get(handles.h.GamLPcPow, 'String'))...
             str2num(get(handles.h.GamHPcPow, 'String'))   str2num(get(handles.h.GamVHPcPow, 'String'))   str2num(get(handles.h.Cust1PcPow, 'String'))...
             str2num(get(handles.h.Cust2PcPow, 'String'))   str2num(get(handles.h.Cust3PcPow, 'String'))];
        m = length(GetThese);

        P = Pows(GetThese);
        PP = PcPows(GetThese);
        
        Labels = {'1 - 4Hz', '4 - 8Hz', '8 - 14Hz', '14 - 30Hz',...
            '30 - 50Hz', '50 - 100Hz', '100 - 150Hz'};
        for ii = 1:m
            G{ii} = Labels{GetThese(ii)};
        end
        if handles.h.CB
            %If there are user-defined bands
            Cb =handles.h.custombands;
            if(length(Cb)>0)
                Labels{8} = strcat(num2str(Cb(1)), '-', num2str(Cb(2), 'Hz') );
            end
            if(length(Cb)>2)
                Labels{8} = strcat(num2str(Cb(3)), '-', num2str(Cb(4), 'Hz') );
            end
            if(length(Cb)>4)
                Labels{8} = strcat(num2str(Cb(5)), '-', num2str(Cb(6), 'Hz'));
            end
        end
        
       
        Exp{1, 1} = 'Range';
        Exp{2, 1} = 'Power';
        Exp{3, 1} = 'Percentage of Total Power';
        for ii = 1:m
            Exp{1, ii+1} = G{ii};
            Exp{2, ii+1} = P(ii);
            Exp{3, ii+1} = PP(ii);
        end
        
       
        s = xlswrite(get(handles.FPath, 'String'),Exp, get(handles.WSheet, 'String'))
        if(s)
            helpdlg('Your data was exported succsfully','Successful Export');
            delete(gcf)

        else
            errordlg('There was a problem while exporting your data, please try again','Export Fail');
        end
    end
else
    errordlg('Please enter a filepath ending in .xls','Export Fail');
end

        
