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

% Copyright 2002-2003 The MathWorks, Inc.

% Edit the above text to modify the response to help resolution

% Last Modified by GUIDE v2.5 02-Jan-2006 14:23:17

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @resolution_OpeningFcn, ...
                   'gui_OutputFcn',  @resolution_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 resolution is made visible.
function resolution_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.

set(hObject, 'Units', 'pixels');

position = get(hObject, 'Position');
set(hObject, 'Position', [50 50 400 380]);

if exist ('vals.mat')==2
    load ('vals.mat');

set(handles.fileedit,'String',resdir);

if nperiods==144
    set(handles.periodspop,'Value',1);
else
    set(handles.periodspop,'Value',2);
end

string_number = get(handles.segmentspop,'String');
for x=1:2
    number_vergleich = str2num(string_number{x});
    if number_vergleich == segmentno
        set(handles.segmentspop,'Value',x);
    end
end

string_size = get(handles.sizepop,'String');
for x=1:2
    size_vergleich = str2num(string_size{x});
    if size_vergleich == size_part
        set(handles.sizepop,'Value',x);
    end
end


set(handles.commabox,'Value',comma);

end
% Choose default command line output for resolution
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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

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

function fileedit_Callback(hObject, eventdata, handles)

function fileedit_CreateFcn(hObject, eventdata, handles)

if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

% --- Executes on button press in browsefile.
function browsefile_Callback(hObject, eventdata, handles)

if exist(get(handles.fileedit,'String'),'file') == 0 %if the file in file_edit does not exist
    init_dir='C:\'; % directory to start with
else
    [pathstr,name,ext] = fileparts(get(handles.fileedit,'String')); % components of the file and directory
    init_dir=[pathstr,'\'];
end

[filename,filedir]=uigetfile({'*.jpg','JPEG (*.jpg)';'*.tif','TIFF (*.tif)';'*.bmp','BMP (*.bmp)';'*.*','All Files (*.*)'},'Select a file',init_dir);; % call for the function to open a file from windows

if filename == 0 & filedir == 0
    set(handles.fileedit,'String',['Invalid filename !']); % if the file does not exist
elseif exist([filedir,filename],'file') == 2 %if the file does exist
    set(handles.fileedit,'String',[filedir,filename]); %if the file exists
end
 if (get(handles.sameasbox,'Value') == get(handles.sameasbox,'Max'))
                    set(handles.diredit,'String',filedir);
  end


% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)

%Start %%%%%%%%

string = get(handles.roiedit,'String');
starcut = str2num(string);

val = get(handles.periodspop,'Value');
string = get(handles.periodspop,'String');
nperiods = str2num(string{val});

val = get(handles.sizepop,'Value');
string = get(handles.sizepop,'String');
size_part = str2num(string{val});

val = get(handles.segmentspop,'Value');
string = get(handles.segmentspop,'String');
segmentno = str2num(string{val});

comma=get(handles.commabox,'Value');

if exist(get(handles.diredit,'String'),'dir') == 0 %if the directory in dir_edit does not exist
        errordlg('Invalid Destination Directory - set to c:\ ');
        set(handles.diredit,'String','C:\')
else
        resdir = get(handles.diredit,'String');
end

[filedir,filename,ext] = fileparts(get(handles.fileedit,'String'));

if exist ([filedir,'\',filename,'_','FIT_S','.txt'],'file') == 2 %deletes txt-file for results if it exists already
                    delete ([filedir,'\',filename,'_','FIT_S','.txt']);
end

save ('vals.mat','resdir','segmentno','nperiods','size_part','comma','starcut');
resolution_main(filedir,filename,ext,resdir,segmentno,nperiods,size_part,comma,starcut);

% --- Executes on button press in browsedir.
function browsedir_Callback(hObject, eventdata, handles)
% hObject    handle to browsedir (see GCBO)

if exist(get(handles.diredit,'String'),'dir') == 0 %if the directory in dir_edit does not exist
    start_dir='C:\'; % directory to start with
else
    start_dir=get(handles.diredit,'String');
end

end_dir=uigetdir(start_dir,'Select a folder'); % call for the function to chosse a directory 
if end_dir == 0
    set(handles.diredit,'String',['You have not chosen a destination directory!']); % if the directory does not exist
elseif exist(end_dir,'dir') == 7 %if the directory does exist
    set(handles.diredit,'String',end_dir); %if the directory exists
else
    errordlg('You have chosen an invalid directory!','Error'); % if the directory does not exist
end

% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox1 (see GCBO)


function diredit_Callback(hObject, eventdata, handles)
% hObject    handle to diredit (see GCBO)

% --- Executes during object creation, after setting all properties.
function diredit_CreateFcn(hObject, eventdata, handles)

if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in segmentspop.
function segmentspop_Callback(hObject, eventdata, handles)
% hObject    handle to segmentspop (see GCBO)

% --- Executes during object creation, after setting all properties.
function segmentspop_CreateFcn(hObject, eventdata, handles)
% hObject    handle to segmentspop (see GCBO)

if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in sizepop.
function sizepop_Callback(hObject, eventdata, handles)
% hObject    handle to sizepop (see GCBO)


% --- Executes during object creation, after setting all properties.
function sizepop_CreateFcn(hObject, eventdata, handles)
% hObject    handle to sizepop (see GCBO)

if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in periodspop.
function periodspop_Callback(hObject, eventdata, handles)
% hObject    handle to periodspop (see GCBO)

% --- Executes during object creation, after setting all properties.
function periodspop_CreateFcn(hObject, eventdata, handles)
% hObject    handle to periodspop (see GCBO)

if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox2 (see GCBO)

% --- Executes on button press in commabox.
function commabox_Callback(hObject, eventdata, handles)
% hObject    handle to commabox (see GCBO)


function roiedit_Callback(hObject, eventdata, handles)
% hObject    handle to roiedit (see GCBO)


% --- Executes during object creation, after setting all properties.
function roiedit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to roiedit (see GCBO)

if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on button press in help.
function help_Callback(hObject, eventdata, handles)
% hObject    handle to help (see GCBO)

IEResolutionsinglehelp;

% --- Executes on button press in corrbox.
function corrbox_Callback(hObject, eventdata, handles)
% hObject    handle to corrbox (see GCBO)


% --- Executes on button press in close.
function close_Callback(hObject, eventdata, handles)
% hObject    handle to close (see GCBO)

close(gcf);
