%
% Author: Javier Lopez-Calderon
% Center for Mind and Brain
% University of California, Davis,
% Davis, CA
% 2012

%b8d3721ed219e65100184c6b95db209bb8d3721ed219e65100184c6b95db209b
%
% ERPLAB Toolbox
% Copyright © 2007 The Regents of the University of California
% Created by Javier Lopez-Calderon and Steven Luck
% Center for Mind and Brain, University of California, Davis,
% javlopez@ucdavis.edu, sjluck@ucdavis.edu
%
% 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 3 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, see <http://www.gnu.org/licenses/>.

function varargout = meaviewerGUI(varargin)

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @meaviewerGUI_OpeningFcn, ...
                   'gui_OutputFcn',  @meaviewerGUI_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 meaviewerGUI is made visible.
function meaviewerGUI_OpeningFcn(hObject, eventdata, handles, varargin)


% Choose default command line output for meaviewerGUI
handles.output = [];

try
        ALLERP    = varargin{1};
        AMP       = varargin{2};
        Lat       = varargin{3};
        binArray  = varargin{4};
        chanArray = varargin{5};
        setArray  = varargin{6};
        latency   = varargin{7};
        blc       = varargin{8};
        moption   = varargin{9};
        tittle    = varargin{10};
        
catch
        ALLERP    = [];
        AMP       = [];
        Lat       = [];
        binArray  = [];
        chanArray = [];
        setArray  = [];
        latency   = [];
        blc       = [];
        moption   = '';
        tittle    = 'nada';
end

cwm = erpworkingmemory('WMColor');
cvl = erpworkingmemory('VLColor');

if isempty(cwm)
        cwm = [0.8490    1.0000    0.1510];
end
if isempty(cvl)
        cvl = [1 0 0];
end

handles.cwm       = cwm;
handles.cvl       = cvl;
handles.ALLERP    = ALLERP;
handles.AMP       = AMP;
handles.Lat       = Lat;
handles.binArray  = binArray;
handles.chanArray = chanArray;
handles.setArray  = setArray;
handles.ich       = 1;
handles.ibin      = 1;
handles.iset      = 1;
handles.latency   = latency;
handles.blc       = blc;
handles.moption   = moption;
handles.tittle    = tittle;
handles.measurearray = {'Instantaneous amplitude','Mean amplitude between two fixed latencies',...
            'Peak amplitude','Peak latency','Fractional Peak latency',...
            'Numerical integration/Area between two fixed latencies',...
            'Numerical integration/Area between two (automatically detected) zero-crossing latencies'...
            'Fractional Area latency'};
handles.meacodes    =      {'instabl', 'meanbl', 'peakampbl', 'peaklatbl', 'fpeaklat',...
                  'areat', 'areap', 'arean','areazt','areazp','areazn','fareatlat',...
                  'fareaplat','fninteglat','fareanlat', 'ninteg','nintegz' };

%
% Name & version
%
version = geterplabversion;
set(handles.figure1,'Name', ['ERPLAB ' version '   -   VIEWER FOR MEASUREMENTS GUI'])

ibin  = 1;
ich   = 1;
iset  = 1;
times = ALLERP(1).times;
ylim  = [-20 20];
xlim  = [min(times) max(times)];

set(handles.edit_ylim, 'String', num2str(ylim)) 
set(handles.edit_xlim, 'String', sprintf('%g %g', round(xlim)))

set(handles.edit_bin, 'String', num2str(ibin)) 
set(handles.edit_channel, 'String', num2str(ich)) 
set(handles.edit_file, 'String', num2str(iset)) 
word = 'positive';
set(handles.togglebutton_y_axis_polarity, 'String', sprintf('<HTML><center><b>%s</b> is up', word));
handles.ydir = 'normal';

%
% Color GUI
%
handles = painterplab(handles);

%
% Set font size
%
handles = setfonterplab(handles);

% Update handles structure
guidata(hObject, handles);

% help
% helpbutton

%
% Plot figure
%
plotdata(handles, ibin, ich, iset, xlim, ylim, tittle)

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

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

% Get default command line output from handles structure
varargout{1} = handles.output;

% The figure can be deleted now
delete(handles.figure1);
pause(0.1)


% -------------------------------------------------------------------------
function pushbutton_left_bin_Callback(hObject, eventdata, handles)
tittle = handles.tittle;
ich    = handles.ich;
ibin   = handles.ibin;
iset   = handles.iset;

ibin = ibin-1;

if ibin<1
      ibin = 1;
end

ylim = str2num(get(handles.edit_ylim, 'String' ));
xlim = str2num(get(handles.edit_xlim, 'String' ));
plotdata(handles, ibin, ich, iset, xlim, ylim, tittle)

%handles.ich       = 1;
handles.ibin      = ibin;
%handles.iset      = 1;

% Update handles structure
guidata(hObject, handles);

% checknan(hObject, eventdata, handles)
% -------------------------------------------------------------------------
function pushbutton_right_bin_Callback(hObject, eventdata, handles)
tittle    = handles.tittle;
binArray  = handles.binArray;
ich    = handles.ich;
ibin   = handles.ibin;
iset   = handles.iset;

ibin = ibin+1;

if ibin>length(binArray)
      ibin = length(binArray);
end

ylim = str2num(get(handles.edit_ylim, 'String' ));
xlim = str2num(get(handles.edit_xlim, 'String' ));
plotdata(handles, ibin, ich, iset, xlim, ylim, tittle)

%handles.ich       = 1;
handles.ibin      = ibin;
%handles.iset      = 1;

% Update handles structure
guidata(hObject, handles);

% checknan(hObject, eventdata, handles)

% -------------------------------------------------------------------------
function pushbutton_left_channel_Callback(hObject, eventdata, handles)
tittle    = handles.tittle;
ich    = handles.ich;
ibin   = handles.ibin;
iset   = handles.iset;

ich = ich-1;

if ich<1
      ich = 1;
end

ylim = str2num(get(handles.edit_ylim, 'String' ));
xlim = str2num(get(handles.edit_xlim, 'String' ));
plotdata(handles, ibin, ich, iset, xlim, ylim, tittle)

handles.ich       = ich;
%handles.ibin      = ibin;
%handles.iset      = 1;

% Update handles structure
guidata(hObject, handles);

% checknan(hObject, eventdata, handles)

% -------------------------------------------------------------------------
function pushbutton_right_channel_Callback(hObject, eventdata, handles)
tittle    = handles.tittle;
chanArray = handles.chanArray;
ich    = handles.ich;
ibin   = handles.ibin;
iset   = handles.iset;

ich = ich+1;

if ich>length(chanArray)
      ich = length(chanArray);
end

ylim = str2num(get(handles.edit_ylim, 'String' ));
xlim = str2num(get(handles.edit_xlim, 'String' ));
plotdata(handles, ibin, ich, iset, xlim, ylim, tittle)

handles.ich       = ich;

% Update handles structure
guidata(hObject, handles);

% checknan(hObject, eventdata, handles)
% -------------------------------------------------------------------------
function pushbutton_left_file_Callback(hObject, eventdata, handles)
tittle    = handles.tittle;
ich    = handles.ich;
ibin   = handles.ibin;
iset   = handles.iset;

iset = iset-1;

if iset<1
      iset = 1;
end

ylim = str2num(get(handles.edit_ylim, 'String' ));
xlim = str2num(get(handles.edit_xlim, 'String' ));
plotdata(handles, ibin, ich, iset, xlim, ylim, tittle)

handles.iset      = iset;

% Update handles structure
guidata(hObject, handles);

% checknan(hObject, eventdata, handles)

% -------------------------------------------------------------------------
function pushbutton_right_file_Callback(hObject, eventdata, handles)
tittle = handles.tittle;
ALLERP = handles.ALLERP;
setArray = handles.setArray;
ich    = handles.ich;
ibin   = handles.ibin;
iset   = handles.iset;

iset = iset+1;

if iset>length(setArray)
      iset = length(setArray);
end

ylim = str2num(get(handles.edit_ylim, 'String' ));
xlim = str2num(get(handles.edit_xlim, 'String' ));
plotdata(handles, ibin, ich, iset, xlim, ylim, tittle)

handles.iset      = iset;

% Update handles structure
guidata(hObject, handles);

% checknan(hObject, eventdata, handles)

% -------------------------------------------------------------------------
function checknan(handles, AMP, ich, ibin, iset, val)

% AMP   = handles.AMP;
% ich   = handles.ich;
% ibin  = handles.ibin;
% iset  = handles.iset;
% val   = AMP(ibin, ich, iset); % ibin, ich, iset are ok 

if isnan(val)
      set(handles.edit_report, 'BackgroundColor', 'r')
else
      set(handles.edit_report, 'BackgroundColor', 'w')
end

% -------------------------------------------------------------------------
function pushbutton_close_Callback(hObject, eventdata, handles)
handles.output = [];

% Update handles structure
guidata(hObject, handles);
uiresume(handles.figure1);

% -------------------------------------------------------------------------
function pushbutton6_Callback(hObject, eventdata, handles)

% -------------------------------------------------------------------------
function edit_xlim_Callback(hObject, eventdata, handles)

% -------------------------------------------------------------------------
function edit_xlim_CreateFcn(hObject, eventdata, handles)

% 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 edit_ylim_Callback(hObject, eventdata, handles)

% -------------------------------------------------------------------------
function edit_ylim_CreateFcn(hObject, eventdata, handles)

% 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 pushbutton_update_Callback(hObject, eventdata, handles)
tittle = handles.tittle;
ylim   = str2num(get(handles.edit_ylim, 'String' ));
xlim   = str2num(get(handles.edit_xlim, 'String' ));

if isempty(xlim) || isempty(ylim)
      return
end

ich    = handles.ich;
ibin   = handles.ibin;
iset   = handles.iset;

plotdata(handles, ibin, ich, iset, xlim, ylim, tittle)

% -------------------------------------------------------------------------
function edit_bin_Callback(hObject, eventdata, handles)

bin       = str2num(get(handles.edit_bin, 'String')); 
binArray  = handles.binArray;

[c, ibin] = closest(binArray, bin);

%handles.ich       = ich;
handles.ibin      = ibin;
%handles.iset      = iset;

% Update handles structure
guidata(hObject, handles);

set(handles.edit_bin, 'String', num2str(ibin)) 

tittle = handles.tittle;
ylim   = str2num(get(handles.edit_ylim, 'String' ));
xlim   = str2num(get(handles.edit_xlim, 'String' ));

if isempty(xlim) || isempty(ylim)
      return
end

ich    = handles.ich;
iset   = handles.iset;

plotdata(handles, ibin, ich, iset, xlim, ylim, tittle)

% -------------------------------------------------------------------------
function edit_bin_CreateFcn(hObject, eventdata, handles)

% 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 edit_channel_Callback(hObject, eventdata, handles)

chan       = str2num(get(handles.edit_channel, 'String')); 
chanArray  = handles.chanArray;

[c, ich] = closest(chanArray, chan);

handles.ich       = ich;
%handles.ibin      = ibin;
%handles.iset      = iset;

% Update handles structure
guidata(hObject, handles);

set(handles.edit_channel, 'String', num2str(ich)) 

tittle = handles.tittle;
ylim   = str2num(get(handles.edit_ylim, 'String' ));
xlim   = str2num(get(handles.edit_xlim, 'String' ));

if isempty(xlim) || isempty(ylim)
      return
end

ibin   = handles.ibin;
iset   = handles.iset;

plotdata(handles, ibin, ich, iset, xlim, ylim, tittle)
% -------------------------------------------------------------------------
function edit_channel_CreateFcn(hObject, eventdata, handles)

% 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 edit_file_Callback(hObject, eventdata, handles)
setx      = str2num(get(handles.edit_file, 'String')); 
setArray  = handles.setArray;

[c, iset] = closest(setArray, setx);

handles.iset      = iset;

% Update handles structure
guidata(hObject, handles);

set(handles.edit_file, 'String', num2str(iset)) 

tittle = handles.tittle;
ylim   = str2num(get(handles.edit_ylim, 'String' ));
xlim   = str2num(get(handles.edit_xlim, 'String' ));

if isempty(xlim) || isempty(ylim)
      return
end

ich    = handles.ich;
ibin   = handles.ibin;

plotdata(handles, ibin, ich, iset, xlim, ylim, tittle)
% -------------------------------------------------------------------------
function edit_file_CreateFcn(hObject, eventdata, handles)

% 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 edit_report_Callback(hObject, eventdata, handles)

% -------------------------------------------------------------------------
function edit_report_CreateFcn(hObject, eventdata, handles)

% 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 Untitled_1_Callback(hObject, eventdata, handles)

%--------------------------------------------------------------------------
function pushbutton_color_window_Callback(hObject, eventdata, handles)

cwm = handles.cwm;
cwm = uisetcolor(cwm,'ERPLAB Background Color') ;
erpworkingmemory('WMColor', cwm);

handles.cwm = cwm;
% Update handles structure
guidata(hObject, handles);

tittle = handles.tittle;
ylim   = str2num(get(handles.edit_ylim, 'String' ));
xlim   = str2num(get(handles.edit_xlim, 'String' ));

if isempty(xlim) || isempty(ylim)
      return
end

ich    = handles.ich;
ibin   = handles.ibin;
iset   = handles.iset;

plotdata(handles, ibin, ich, iset, xlim, ylim, tittle)
%--------------------------------------------------------------------------
function pushbutton_color_val_Callback(hObject, eventdata, handles)
cvl = handles.cvl;
cvl = uisetcolor( cvl,'ERPLAB Background Color') ;
erpworkingmemory('VLColor', cvl);

handles.cvl = cvl;
% Update handles structure
guidata(hObject, handles);

tittle = handles.tittle;
ylim   = str2num(get(handles.edit_ylim, 'String' ));
xlim   = str2num(get(handles.edit_xlim, 'String' ));

if isempty(xlim) || isempty(ylim)
      return
end

ich    = handles.ich;
ibin   = handles.ibin;
iset   = handles.iset;

plotdata(handles, ibin, ich, iset, xlim, ylim, tittle)

% PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
% PPPPPPPPPPPPPPPPPPPPPPPPPPP   PLOT  PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
% PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
function plotdata(handles, ibin, ich, iset, xlim, ylim, tittle)

ydir      = handles.ydir;
binArray  = handles.binArray;
chanArray = handles.chanArray;
setArray  = handles.setArray;

bin       = binArray(ibin);
channel   = chanArray(ich);
seta      = setArray(iset);

ALLERP    = handles.ALLERP;
AMP       = handles.AMP;
Lat       = handles.Lat;

lat4mea   = Lat{1, 1, iset}; % iset is ok
lat4mea   = lat4mea{ibin, ich};
val       = AMP(ibin, ich, iset); % ibin, ich, iset are ok 

data      = ALLERP(seta).bindata;
times     = ALLERP(seta).times;
latency   = handles.latency;
truelat   = latency;
blc       = handles.blc; 
meacodes  = handles.meacodes;
moption   = handles.moption;
mearea    = { 'areat', 'areap', 'arean','areazt','areazp','areazn', 'ninteg','nintegz'};

axes(handles.axes1);

%
% Baseline
%
blv = blvalue(ALLERP(seta), channel, bin, blc);

%
% Colors
%
cwm = handles.cwm;
cvl = handles.cvl;

%
% Plot
%
plot(times, data(channel, :, bin) - blv, 'LineWidth', 1, 'Color', 'b');
hold on

%
% Line for value
%
latetype = 0; % when lat is from measurement window
switch moption
      case 'instabl'
            line(xlim, [val val], 'Color', cvl, 'LineStyle',':')
            line([latency(1)-(xlim(2)-xlim(1))*0.025 latency(1)+(xlim(2)-xlim(1))*0.025], [val val], 'Color', cvl)
      case {'meanbl', 'areat', 'areap', 'arean', 'ninteg'}
            line(xlim, [val val], 'Color', cvl, 'LineStyle',':')
            line([latency(1) latency(2)], [val val], 'Color', cvl)
      case {'areazt','areazp','areazn', 'nintegz'}
            line(xlim, [val val], 'Color', cvl, 'LineStyle',':')
            %line([latency(1)-(xlim(2)-xlim(1))*0.025 latency(1)+(xlim(2)-xlim(1))*0.025], [val val], 'Color', 'r')
            line([lat4mea(1) lat4mea(2)], [val val], 'Color', cvl)
            latetype = 1; % use true lat istead
            truelat  = lat4mea;
      case 'peakampbl'
            line(xlim, [val val], 'Color', cvl, 'LineStyle',':')
            line([lat4mea(1)-(xlim(2)-xlim(1))*0.025 lat4mea(1)+(xlim(2)-xlim(1))*0.025], [val val], 'Color', cvl)
            truelat = lat4mea;
      otherwise
end

%  'instabl', 'meanbl', 'peakampbl', 'peaklatbl', 'fpeaklat',...
%  'areat', 'areap', 'arean','areazt','areazp','areazn','fareatlat',...
%  'fareaplat','fninteglat','fareanlat', 'ninteg','nintegz' ;

%
% Paint area
%
if ismember(moption, mearea)  || ismember(moption, {'fareatlat', 'fareaplat','fninteglat','fareanlat'})
      
      if latetype ==0
            latx = latency;
      else
            latx = lat4mea;
      end
      
      [xxx, latsamp] = closest(times, latx);
      datax = data(channel, latsamp(1):latsamp(2), bin) - blv;
      
      if ismember(moption, {'areap', 'areazp', 'fareaplat'}) % positive area
            datax(datax<0) = 0;
            area(times(latsamp(1):latsamp(2)), datax, 'FaceColor', 'b')
      elseif ismember(moption, {'arean', 'areazn', 'fareanlat'}) % negative area
            datax(datax>0) = 0;
            area(times(latsamp(1):latsamp(2)), datax, 'FaceColor', 'r')
      elseif ismember(moption, {'ninteg','nintegz', 'fninteglat'}) % integration
            datatemp = datax;
            datatemp(datatemp<0) = 0;
            area(times(latsamp(1):latsamp(2)), datatemp, 'FaceColor', 'b')
            datatemp = datax;
            datatemp(datatemp>0) = 0;
            area(times(latsamp(1):latsamp(2)), datatemp, 'FaceColor', 'r')
      else
            area(times(latsamp(1):latsamp(2)), datax, 'FaceColor', [0.2000 0.1 0])
      end
      % area(times(latsamp(1):latsamp(2)), datax);
      % area(times(latsamp(1):latsamp(2)), datax, 'FaceColor', 'r', 'EdgeColor', 'r')
end

axis([xlim ylim])

%
% Line(s) for latency(ies) and window for measurement
%
if length(latency)==1
      if ismember(moption,  {'areazt','areazp','areazn', 'nintegz'})
            line([latency latency], ylim, 'Color', cvl)
            pp = patch([lat4mea(1) lat4mea(2) lat4mea(2) lat4mea(1)],[ylim(1) ylim(1) ylim(2) ylim(2)], cwm);
            set(pp,'FaceAlpha',0.4, 'EdgeAlpha', 0.4, 'EdgeColor', cwm);
            
            %
            % workaround (redraw axis)
            %
            line([0 0], ylim, 'Color', 'k')
            line(xlim, [0 0], 'Color', 'k')                   
      else
            line([latency latency], ylim, 'Color', cvl)
      end
elseif length(latency)==2
      pp = patch([latency(1) latency(2) latency(2) latency(1)],[ylim(1) ylim(1) ylim(2) ylim(2)], cwm);
      set(pp,'FaceAlpha',0.4, 'EdgeAlpha', 0.4, 'EdgeColor', cwm);
      
      if ismember(moption, {'peakampbl'})
            line([lat4mea lat4mea], ylim, 'Color', cvl)
      elseif ismember(moption, {'peaklatbl', 'fareatlat', 'fareaplat','fninteglat','fareanlat'})
            line([val val], ylim, 'Color', cvl)
      elseif ismember(moption,  {'fpeaklat'}) % fractional peak latency
            line([val val], ylim, 'Color', cvl) % fractional peak lat
            line([lat4mea lat4mea], ylim, 'Color', cvl,'LineStyle',':')  % peak lat
      end
      
      %
      % workaround (redraw axis)
      %
      line([0 0], ylim, 'Color', 'k')
      line(xlim, [0 0], 'Color', 'k')
end

set(handles.axes1,'ydir', ydir);
% axescenter(handles.axes1)
neoaxescenter(handles.axes1)
hold off
drawnow

set(handles.edit_bin, 'String', num2str(bin)) 
set(handles.edit_channel, 'String', num2str(channel)) 
set(handles.edit_file, 'String', num2str(seta)) 

repo = sprintf('File   : %s\nBin    : %s\nChannel: %s\nMeasu  : %s\nWindow : %s\nLate   : %s\nValue  : %s\n',...
      ALLERP(seta).erpname, ALLERP(seta).bindescr{bin}, ALLERP(seta).chanlocs(channel).labels, tittle, num2str(latency), num2str(truelat), num2str(val));
set(handles.edit_report, 'String', repo)
checknan(handles, AMP, ich, ibin, iset, val)

%--------------------------------------------------------------------------
function togglebutton_y_axis_polarity_Callback(hObject, eventdata, handles)

ylim   = str2num(get(handles.edit_ylim, 'String' ));
xlim   = str2num(get(handles.edit_xlim, 'String' ));

if isempty(xlim) || isempty(ylim)
      set(hObject, 'string', sprintf('<HTML><center><b>%s</b> is up', 'positive'));
      set(hObject, 'Value', 1)
      return
end
if get(hObject, 'Value')
      word = 'negative';
      %set(handles.axes1,'ydir','reverse');
      handles.ydir = 'reverse';
else
      word = 'positive';
      %set(handles.axes1,'ydir','normal');
      handles.ydir = 'normal';
end
set(hObject, 'string', sprintf('<HTML><center><b>%s</b> is up', word));

%Update handles structure
guidata(hObject, handles);

tittle = handles.tittle;
ich    = handles.ich;
ibin   = handles.ibin;
iset   = handles.iset;

plotdata(handles, ibin, ich, iset, xlim, ylim, tittle)

%--------------------------------------------------------------------------
function figure1_CloseRequestFcn(hObject, eventdata, handles)

if isequal(get(handles.figure1, 'waitstatus'), 'waiting')
      %The GUI is still in UIWAIT, us UIRESUME
      handles.output = '';
      %Update handles structure
      guidata(hObject, handles);
      uiresume(handles.figure1);
else
      % The GUI is no longer waiting, just close it
      delete(handles.figure1);
end
