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

% Last Modified by GUIDE v2.5 08-May-2012 14:42:08

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

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

% Update handles structure
guidata(hObject, handles);

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


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


%--------------------------------------------------------------------------
%DIRECTORY SELECTION
% --- Executes on button press in directory_push.
function directory_push_Callback(~, ~, handles)
% hObject    handle to directory_push (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
PathName = uigetdir('f:\Bonemodeling\','Select directory');

%generates output string name
str = strcat(PathName);
%sets text in textfield "textfileselction" to the currently selected file
set(handles.directory_text, 'string', str);
%--------------------------------------------------------------------------


%--------------------------------------------------------------------------
%QUIT GUI
% --- Executes on button press in quit_push.
function quit_push_Callback(~, ~, ~)
% hObject    handle to quit_push (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
clc
close all
clear all
%--------------------------------------------------------------------------
 

%--------------------------------------------------------------------------
%START STOP FUNCTION
% --- Executes on button press in start_push.
function start_push_Callback(hObject, eventdata, handles)
% hObject    handle to startstopbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% get the current pushbutton string
str = get(hObject,'String');     
% Determine which string the label matches 
state = strcmp(str, 'START'); 

% If the index when entering was 1, start to operate
if (state == 1)
    % Toggle the button label to other string 
    set(hObject,'String','STOP'); 
    %getting threshold value as double
    thr_selection = get (handles.thr_group, 'SelectedObject');
    thr_string = get (thr_selection, 'String');
    sed_thr = str2double (thr_string);
    %getting directory
    directory = get (handles.directory_text, 'string');
    %getting compare mode via allocating the name to a number in "pictureloop.m"
    cmpr_selection = get (handles.cmpr_group, 'SelectedObject');
    cmpr_string = get (cmpr_selection, 'TooltipString');
    compare = str2double (cmpr_string);

    %Showing executed line
    set (handles.command_text, 'String', {'Command: ' ['pictureloop (' thr_string ', ' directory ', ' cmpr_string ')']});
    
    % Disable the other key.
    set(handles.quit_push,'Enable','off');
    set(handles.directory_push,'Enable','off');
    set(handles.thr_1_radio,'Enable','off');
    set(handles.thr_5_radio,'Enable','off');
    set(handles.thr_10_radio,'Enable','off');
    set(handles.cmpr_i_radio,'Enable','off');
    set(handles.cmpr_p_radio,'Enable','off');
    set(handles.cmpr_f_radio,'Enable','off');
    
    %pausing to show the executeing line and to avoid lag due to starting programm
    pause (1) 
    set (handles.command_text, 'string', 'Running');
    
    pictureloop (sed_thr, directory, compare)
    %cd ('F:\Test'); %Probe, ob Folder geändert werden muss um Quit und Stop button nutzbar zu machen

elseif (state == 0)
    % Toggle the button label to other string 
    return
    
    set(hObject,'String','START');
    set (handles.command_text, 'string', 'Cancelled');
    % Disable the other key.
    set(handles.quit_push,'Enable','on');
    set(handles.directory_push,'Enable','on');
    set(handles.thr_1_radio,'Enable','on');
    set(handles.thr_5_radio,'Enable','on');
    set(handles.thr_10_radio,'Enable','on');
    set(handles.cmpr_i_radio,'Enable','on');
    set(handles.cmpr_p_radio,'Enable','on');
    set(handles.cmpr_f_radio,'Enable','on');
    
    %cd ('F:\Test'); %Probe, ob Folder geändert werden muss um Quit und Stop button nutzbar zu machen
 
else
    disp ('ERROR: Something wrong with the Start/Stop Button');
    disp('Take a break, get yourself a coffee and think about what you´ve done!');
    
end

a = 1;
b = 2;
while a == 1
    b = b+1;
    pause (1)
end
