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

% Last Modified by GUIDE v2.5 09-Dec-2006 10:09:02

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

% Add fields for data exchange
handles.param_file{1} = '';

% Update handles structure
guidata(hObject, handles);

%Read the existing parameter file 
fid = fopen('hall_probe_calib_param.m', 'r');
ii = 0;
while ~feof(fid)
    ii = ii + 1;
    param_file{ii} = fgets(fid);
end
fclose(fid);

%Get the input file directory
for ii = 1 : length(param_file)
    if findstr('INPUT_FILE_DIR', param_file{ii}) > 0
        index_array = findstr('''', param_file{ii});
        input_file_dir = param_file{ii}(index_array(1) + 1 : index_array(2) - 1);
        break;
    end
end

%Get the input file name
for ii = 1 : length(param_file)
    if findstr('INPUT_FILE_NAME', param_file{ii}) > 0
        index_array = findstr('''', param_file{ii});
        input_file_name = param_file{ii}(index_array(1) + 1 : index_array(2) - 1);
        break;
    end
end

%Get the polynomial fit order
for ii = 1 : length(param_file)
    if findstr('POLY_FIT_ORDER', param_file{ii}) > 0
        index_array_1 = findstr('=', param_file{ii});
        index_array_2 = findstr(';', param_file{ii});
        poly_fit_order = str2num(param_file{ii}(index_array_1 + 2 : index_array_2 - 1));
        break;
    end
end

%Set the edit boxes to the default parameter values
set(handles.txt_input_file_dir, 'string', input_file_dir);
set(handles.txt_input_file_name, 'string', input_file_name);
set(handles.txt_poly_fit_order, 'string', num2str(poly_fit_order));

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


% --- Outputs from this function are returned to the command line.
function varargout = hall_probe_calib_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)

% Return the program parameters
varargout{1} = handles.param_file;

%Close the user interface
delete(handles.figure1);



function txt_input_file_dir_Callback(hObject, eventdata, handles)
% hObject    handle to txt_input_file_dir (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 txt_input_file_dir as text
%        str2double(get(hObject,'String')) returns contents of txt_input_file_dir as a double


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


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


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

%Get the contents of the edit boxes
input_file_dir = get(handles.txt_input_file_dir, 'string');
input_file_name = get(handles.txt_input_file_name, 'string');
poly_fit_order = str2num(get(handles.txt_poly_fit_order, 'string'));

%Read the parameter file 
fid = fopen('hall_probe_calib_param.m', 'r');
ii = 0;
while ~feof(fid)
    ii = ii + 1;
    param_file{ii} = fgets(fid);
end
fclose(fid);

%Save a backup copy of the parameter file 
fid = fopen('hall_probe_calib_param_backup.m', 'w');
for ii = 1 : length(param_file)
    fprintf(fid, '%s', param_file{ii});
end
fclose(fid);

%Delete the parameter file so the old version can not be used
delete('hall_probe_calib_param.m');

%Update the input file directory
for ii = 1 : length(param_file)
    if findstr('INPUT_FILE_DIR', param_file{ii}) > 0
        index_array = findstr('''', param_file{ii});
        param_file{ii} = [param_file{ii}(1 : index_array(1)), input_file_dir, param_file{ii}(index_array(2) : end)];
        break;
    end
end

%Update the input file name
for ii = 1 : length(param_file)
    if findstr('INPUT_FILE_NAME', param_file{ii}) > 0
        index_array = findstr('''', param_file{ii});
        param_file{ii} = [param_file{ii}(1 : index_array(1)), input_file_name, param_file{ii}(index_array(2) : end)];
        break;
    end
end

%Update the polynomial fit order
for ii = 1 : length(param_file)
    if findstr('POLY_FIT_ORDER', param_file{ii}) > 0
        index_array_1 = findstr('=', param_file{ii});
        index_array_2 = findstr(';', param_file{ii});
        param_file{ii} = [param_file{ii}(1 : index_array_1), ' ', num2str(poly_fit_order), param_file{ii}(index_array_2 : end)];
        break;
    end
end

%Write the updated parameter file 
fid = fopen('hall_probe_calib_param.m', 'w');
for ii = 1 : length(param_file)
    fprintf(fid, '%s', param_file{ii});
end
fclose(fid);

%Wait until the file is written to the disk
while ~exist('hall_probe_calib_param.m', 'file'), end;

%Return the parameter file contents
handles.param_file = param_file;
guidata(hObject, handles);

%Exit the user interface
uiresume(handles.figure1);

%Done
