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

% Last Modified by GUIDE v2.5 07-Jun-2012 01:50:07

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

% Choose default command line output for FIT
Set_Sliders;

load('V_ceLL');
handles.V_cell = V_cell;
load('GEOM_DATA');
handles.Apical_Dist_Mat = (Apical + fliplr(Apical))/2;
handles.Apical_Dist = handles.Apical_Dist_Mat(1, :);

set(handles.V_cell_id_slider, 'Min', 1);
set(handles.V_cell_id_slider, 'Value', 1);
set(handles.V_cell_id_slider, 'Max', 26);
set(handles.V_cell_id_slider, 'Sliderstep', [1/25 3/25]);

handles.V_cell_id = round(get(handles.V_cell_id_slider, 'Value'));
set(handles.V_cell_id_disp, 'String', num2str(handles.V_cell_id));

set(handles.rotate_slider, 'Min', -pi);
set(handles.rotate_slider, 'Value', 0);
set(handles.rotate_slider, 'Max', pi);
set(handles.rotate_slider, 'Sliderstep', [1/1000 10/1000]);

handles.rotate = get(handles.rotate_slider, 'Value');
set(handles.rotate_disp, 'String', handles.rotate);


Plot_Par;
Fit_Atempt;

handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = FIT_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 A_sig_value_Callback(hObject, eventdata, handles)
Fit_Atempt;
guidata(hObject, handles);

function A_sig_min_Callback(hObject, eventdata, handles);
Set_Sliders;
guidata(hObject, handles);

function A_sig_max_Callback(hObject, eventdata, handles)
Set_Sliders;
guidata(hObject, handles);



function A_mag_value_Callback(hObject, eventdata, handles)
Fit_Atempt;
guidata(hObject, handles);

function A_mag_min_Callback(hObject, eventdata, handles)
Set_Sliders;
guidata(hObject, handles);

function A_mag_max_Callback(hObject, eventdata, handles)
Set_Sliders;
guidata(hObject, handles);



function B_mag_value_Callback(hObject, eventdata, handles)
Fit_Atempt;
guidata(hObject, handles);

function B_mag_min_Callback(hObject, eventdata, handles)
Set_Sliders;
guidata(hObject, handles);

function B_mag_max_Callback(hObject, eventdata, handles)
Set_Sliders;
guidata(hObject, handles);


function B_radius_value_Callback(hObject, eventdata, handles)
Fit_Atempt;
guidata(hObject, handles);

function B_radius_min_Callback(hObject, eventdata, handles)
Set_Sliders;
guidata(hObject, handles);

function B_radius_max_Callback(hObject, eventdata, handles)
Set_Sliders;
guidata(hObject, handles);

function W_mag_value_Callback(hObject, eventdata, handles)
Set_Sliders;
guidata(hObject, handles);

function W_mag_min_Callback(hObject, eventdata, handles)
Set_Sliders;
guidata(hObject, handles);

function W_mag_max_Callback(hObject, eventdata, handles)
Set_Sliders;
guidata(hObject, handles);

% --- Executes on slider movement.
function A_sig_slider_Callback(hObject, eventdata, handles)
Value = get(handles.A_sig_slider, 'Value');
set(handles.A_sig_value, 'String', num2str(Value));
Fit_Atempt;
guidata(hObject, handles);

% --- Executes on slider movement.
function A_mag_slider_Callback(hObject, eventdata, handles)
Value = get(handles.A_mag_slider, 'Value');
set(handles.A_mag_value, 'String', num2str(Value));
Fit_Atempt;
guidata(hObject, handles);

% --- Executes on slider movement.
function B_mag_slider_Callback(hObject, eventdata, handles)
Value = get(handles.B_mag_slider, 'Value');
set(handles.B_mag_value, 'String', num2str(Value));
Fit_Atempt;
guidata(hObject, handles);

% --- Executes on slider movement.
function B_radius_slider_Callback(hObject, eventdata, handles)
Value = get(handles.B_radius_slider, 'Value');
set(handles.B_radius_value, 'String', num2str(Value));
Fit_Atempt;
guidata(hObject, handles);

% --- Executes on slider movement.
function W_mag_slider_Callback(hObject, eventdata, handles)
Value = get(handles.W_mag_slider, 'Value');
set(handles.W_mag_value, 'String', num2str(Value));
Fit_Atempt;
guidata(hObject, handles);

% --- Executes on slider movement.
function V_cell_id_slider_Callback(hObject, eventdata, handles)
handles.V_cell_id = round(get(handles.V_cell_id_slider, 'Value'));
handles.Apical_Dist = handles.Apical_Dist_Mat(handles.V_cell_id, :);
set(handles.V_cell_id_disp, 'String', num2str(handles.V_cell_id));
Plot_Par;
Fit_Atempt;
guidata(hObject, handles);


% --- Executes on slider movement.
function rotate_slider_Callback(hObject, eventdata, handles)
handles.rotate = get(handles.rotate_slider, 'Value');
set(handles.rotate_disp, 'String', handles.rotate);
Fit_Atempt;
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function A_sig_value_CreateFcn(hObject, eventdata, handles)
% hObject    handle to A_sig_value (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 during object creation, after setting all properties.
function A_mag_CreateFcn(hObject, eventdata, handles)
% hObject    handle to A_mag (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 during object creation, after setting all properties.
function B_mag_CreateFcn(hObject, eventdata, handles)
% hObject    handle to B_mag (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 during object creation, after setting all properties.
function B_radius_CreateFcn(hObject, eventdata, handles)
% hObject    handle to B_radius (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 during object creation, after setting all properties.
function A_sig_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to A_sig_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

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


% --- Executes during object creation, after setting all properties.
function A_sig_max_CreateFcn(hObject, eventdata, handles)
% hObject    handle to A_sig_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 during object creation, after setting all properties.
function A_mag_value_CreateFcn(hObject, eventdata, handles)
% hObject    handle to A_mag_value (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 during object creation, after setting all properties.
function A_mag_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to A_mag_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end




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


% --- Executes during object creation, after setting all properties.
function A_mag_max_CreateFcn(hObject, eventdata, handles)
% hObject    handle to A_mag_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 during object creation, after setting all properties.
function B_radius_value_CreateFcn(hObject, eventdata, handles)
% hObject    handle to B_radius_value (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 during object creation, after setting all properties.
function B_radius_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to B_radius_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end




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


% --- Executes during object creation, after setting all properties.
function B_radius_max_CreateFcn(hObject, eventdata, handles)
% hObject    handle to B_radius_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 during object creation, after setting all properties.
function B_mag_value_CreateFcn(hObject, eventdata, handles)
% hObject    handle to B_mag_value (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 during object creation, after setting all properties.
function B_mag_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to B_mag_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


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


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



function A_par1_Callback(hObject, eventdata, handles)
Plot_Par;
Fit_Atempt;
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function A_par1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to A_par1 (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 A_par2_Callback(hObject, eventdata, handles)
Plot_Par;
Fit_Atempt;
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function A_par2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to A_par2 (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 A_par3_Callback(hObject, eventdata, handles)
Plot_Par;
Fit_Atempt;
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function A_par3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to A_par3 (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 D_size_Callback(hObject, eventdata, handles)
Plot_Par;
Fit_Atempt;
guidata(hObject, handles);



% --- Executes during object creation, after setting all properties.
function D_size_CreateFcn(hObject, eventdata, handles)
% hObject    handle to D_size (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 during object creation, after setting all properties.
function V_cell_id_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to V_cell_id_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

% --- Executes during object creation, after setting all properties.
function rotate_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to rotate_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end



% --- Executes during object creation, after setting all properties.
function W_mag_value_CreateFcn(hObject, eventdata, handles)
% hObject    handle to W_mag_value (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 during object creation, after setting all properties.
function W_mag_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to W_mag_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end




% hObject    handle to W_mag_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 W_mag_min as text
%        str2double(get(hObject,'String')) returns contents of W_mag_min as a double


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





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