%how to use this:
%first run a draft analysis
% CleaningTimes{size(dffold,2)}=[]; nstd_th=zeros(1,size(dff,2));   %initiate
%[CleaningTimes badcells nstd_th]=CleaningCells_UI(badcells,tt1,dffold,tt1_ds,dff_ds,c,s,CleaningTimes,nstd_th,stddff);
% save('CleaningTimes.mat','CleaningTimes','-v7.3')

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

% Last Modified by GUIDE v2.5 15-Feb-2020 14:13:59



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

% Call the data to plot.
handles.badcells=varargin{1};
handles.tt1=varargin{2};
handles.dffold=varargin{3};
handles.tt1_ds=varargin{4};
handles.dff_ds=varargin{5};
handles.c=varargin{6};
handles.s=varargin{7};
handles.CleaningTimes=varargin{8};
handles.nstd_th=varargin{9};
handles.stddff=varargin{10};


% Set the current data value.
handels.current_data_1=handles.dffold(:,1);
plot(handles.axes1,handles.tt1,handels.current_data_1)

handels.current_data_2=handles.dff_ds(:,1);
handels.current_data_3=handles.c{1};
handels.current_data_4=handles.s{1};


hold on;
plot(handles.axes2,handles.tt1_ds,handels.current_data_2)
plot(handles.axes2,handles.tt1_ds,handels.current_data_3)
idx = find(handels.current_data_4); 
plot(handles.axes2,idx/30*3,3*handels.current_data_4(idx),'o','MarkerSize',5)
hold off;


%set the listbox
cellsIDstr={};
cellsID=[];
for i=1:size(handles.dffold,2)
if ~ismember(i,handles.badcells)    
    cellsIDstr=[cellsIDstr num2str(i)];
    cellsID=[cellsID i];
end
end
set(handles.listbox1,'string',cellsIDstr);
handles.cellsID=cellsID;
%another current data setting
handles.currentcellsID=cellsID(1);


% Choose output for CleaningCells_UI
handles.output1 = handles.CleaningTimes;
handles.output2=handles.badcells;
handles.output3=handles.nstd_th;

% Update handles structure
guidata(hObject, handles);
%wait for user input
uiwait()

% UIWAIT makes CleaningCells_UI wait for user response (see UIRESUME)
% uiwait(handles.axes2);


% --- Outputs from this function are returned to the command line.
function varargout = CleaningCells_UI_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.output1;
varargout{2} = handles.output2;
varargout{3} = handles.output3;







% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x=ginput(2);
line(x,max(-0.5,0.95*min(ylim))*[1 1],'LineWidth',1.5,'color','red');
handles.CleaningTimes{handles.currentcellsID}=[handles.CleaningTimes{handles.currentcellsID};[x(1) x(2)]];
handles.output1=handles.CleaningTimes;
guidata(hObject,handles);

    


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

index= get(handles.listbox1,'value');
index=handles.cellsID(index);
handles.currentcellsID=index;

% Set the current data value.
handels.current_data_1=handles.dffold(:,index);
plot(handles.axes1,handles.tt1,handels.current_data_1)

handels.current_data_2=handles.dff_ds(:,index);
handels.current_data_3=handles.c{index};
handels.current_data_4=handles.s{index};
handels.current_data_5=handles.nstd_th(index);
handels.current_data_6=handles.stddff(index);

plot(handles.axes2,handles.tt1_ds,handels.current_data_2)
hold on;
plot(handles.axes2,handles.tt1_ds,handels.current_data_3)
idx = find(handels.current_data_4); 
plot(handles.axes2,idx/30*3,3*handels.current_data_4(idx),'o','MarkerSize',5)
line(xlim,handels.current_data_5*handels.current_data_6*[1 1],'color','g')
line(xlim,-1*handels.current_data_5*handels.current_data_6*[1 1],'color','g')
for i=1:size(handles.CleaningTimes{index},1)
    line(handles.CleaningTimes{index}(i,:),max(-0.5,0.95*min(ylim))*[1 1],'LineWidth',1.5,'color','red');
end
hold off;

guidata(hObject, handles);


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


% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox1 (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


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


% --------------------------------------------------------------------
function uitoggletool1_OnCallback(hObject, eventdata, handles)
% hObject    handle to uitoggletool1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
zoom on;


% --------------------------------------------------------------------
function uitoggletool2_OnCallback(hObject, eventdata, handles)
% hObject    handle to uitoggletool2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
zoom on;


% --------------------------------------------------------------------
function uitoggletool3_OnCallback(hObject, eventdata, handles)
% hObject    handle to uitoggletool3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
pan on;


% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.badcells=[handles.badcells handles.currentcellsID];
handles.output2=handles.badcells;
guidata(hObject,handles);



% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.nstd_th(handles.currentcellsID)=handles.nstd_th(handles.currentcellsID)+0.1;
handles.output3=handles.nstd_th;

index=handles.currentcellsID;

% Set the current data value.
handels.current_data_1=handles.dffold(:,index);
plot(handles.axes1,handles.tt1,handels.current_data_1)

handels.current_data_2=handles.dff_ds(:,index);
handels.current_data_3=handles.c{index};
handels.current_data_4=handles.s{index};
handels.current_data_5=handles.nstd_th(index);
handels.current_data_6=handles.stddff(index);

plot(handles.axes2,handles.tt1_ds,handels.current_data_2)
hold on;
plot(handles.axes2,handles.tt1_ds,handels.current_data_3)
idx = find(handels.current_data_4); 
plot(handles.axes2,idx/30*3,3*handels.current_data_4(idx),'o','MarkerSize',5)
line(xlim,handels.current_data_5*handels.current_data_6*[1 1],'color','g')
line(xlim,-1*handels.current_data_5*handels.current_data_6*[1 1],'color','g')
for i=1:size(handles.CleaningTimes{index},1)
    line(handles.CleaningTimes{index}(i,:),max(-0.5,0.95*min(ylim))*[1 1],'LineWidth',1.5,'color','red');
end
hold off;

guidata(hObject,handles);


% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.nstd_th(handles.currentcellsID)=handles.nstd_th(handles.currentcellsID)-0.1;
handles.output3=handles.nstd_th;

index=handles.currentcellsID;

% Set the current data value.
handels.current_data_1=handles.dffold(:,index);
plot(handles.axes1,handles.tt1,handels.current_data_1)

handels.current_data_2=handles.dff_ds(:,index);
handels.current_data_3=handles.c{index};
handels.current_data_4=handles.s{index};
handels.current_data_5=handles.nstd_th(index);
handels.current_data_6=handles.stddff(index);

plot(handles.axes2,handles.tt1_ds,handels.current_data_2)
hold on;
plot(handles.axes2,handles.tt1_ds,handels.current_data_3)
idx = find(handels.current_data_4); 
plot(handles.axes2,idx/30*3,3*handels.current_data_4(idx),'o','MarkerSize',5)
line(xlim,handels.current_data_5*handels.current_data_6*[1 1],'color','g')
line(xlim,-1*handels.current_data_5*handels.current_data_6*[1 1],'color','g')
for i=1:size(handles.CleaningTimes{index},1)
    line(handles.CleaningTimes{index}(i,:),max(-0.5,0.95*min(ylim))*[1 1],'LineWidth',1.5,'color','red');
end
hold off;

guidata(hObject,handles);
