function varargout = BaSiC_GUI( varargin )
%Folder path: E:\Apps\BaSiC_TL\BaSiC_TLv1.1
%modify the file  BaSiC_config.m through a GUI. 

%   Detailed explanation goes here

clear; close all; clc;

set(0,'units','pixels')
sspix = get(0,'screensize');
H = figure('Position',[sspix(3)*.05 sspix(4)*.05 (sspix(3)*.9) (sspix(4)*.85)],...
    'CloseRequestFcn',@GUI_Close);

global GuIformat GuIdata;

%% Declare Global variables
GuIformat.Vert1 = .96; %defines vertical lines of orientation in GUI
GuIformat.Vert2 = .66;
GuIformat.Vert3 = .6;
GuIformat.Vert4 = 0;
GuIformat.color.c = [255/255 190/255 190/255];
GuIformat.color.o = [102/255 1 1];
GuIformat.color.i = [102/255 1 12/255];

%default paramters
GuIdata.TTTDIR = 'S:\group\Timelapsedata\Timelapsedata\';
GuIdata.movieID= 'default';
GuIdata.lambdaFlatfield_allposition = 2.0;
GuIdata.runStepI = logical(0);
GuIdata.runAllPositionsI = logical(0);
GuIdata.runStepII = logical(0);
GuIdata.runAllPositionsII = logical(0);
GuIdata.useFlatfield_allposition = logical(1); 
GuIdata.positionVectorII = 24;
GuIdata.lambdaFlatfield = [];
GuIdata.lambdaDarkfield = [];
GuIdata.useSegmentation = logical(0);
GuIdata.temporalRangeII=[3 90];
GuIdata.useFlatfield_allposition= logical(1);
GuIdata.runStepIII=logical(0);
GuIdata.runAllPositionsIII = logical(0);
GuIdata.positionVectorIII = 24;
GuIdata.temporalRangeIII = [3 90];
GuIdata.runCorrection = logical(1);
GuIdata.adjustFlatfield= logical(0);
GuIdata.floatOutput = logical(1);

%% Control Panels
movie_information;
StepI;
StepII;
StepIII;
StepIV;
StepV;
end

%% PANEL CONTROL
function movie_information

global GuIformat GuIdata;

Control = uipanel('Visible','on',...
    'Position',[0.25 0.75 .5 .2],... % left bottom edge of panel within parentthen width and height.
    'Title','Movie Information');

%select TTT Folder
    GuIformat.MI(1) = uicontrol(Control,...
    'Units','normalized',...
    'Style','edit',...
    'String','S:\group\Timelapsedata\Timelapsedata\',...
    'ForegroundColor',[0.4,0.4,0.4],...
    'Position',[0.02 0.6 .25 .2],...
    'Callback',@MI_TTTDIR);
    function MI_TTTDIR(hObject,callbackdata) %This Callback stores edited text and sets color to black  
        GuIdata.TTTDIR=get(GuIformat.MI(1),'string'); 
        set(GuIformat.MI(1),'ForegroundColor','black');
    end

    GuIformat.MI(3) = uicontrol(Control,...
    'Units','normalized',...
    'Style','text',...
    'String','patch to your TTT image folder:',...
    'Position',[.02 0.8 .2 .1],...
    'HandleVisibility','off');

    GuIformat.MI(2) = uicontrol(Control,...
    'Units','normalized',...
    'Style','pushbutton',...
    'String','Browse',...
    'Position',[0.27 0.6 .1 .2],...
    'HandleVisibility','off',...
    'BackgroundColor',GuIformat.color.c,...
    'Callback',@MI_Browse);
    function MI_Browse(hObject,callbackdata)  
        GuIdata.TTTDIR=uigetdir(); 
        set(GuIformat.MI(1),'string',GuIdata.TTTDIR);
        set(GuIformat.MI(1),'ForegroundColor','black');
    end


 %Specify Movie ID
    GuIformat.MI(4) = uicontrol(Control,...
    'Units','normalized',...
    'Style','edit',...
    'String','e.g. 141201LS8 ',...
    'ForegroundColor',[0.4,0.4,0.4],...
    'Position',[0.42 0.6 .25 .2],...
    'CallBack',@MI_ID);
    function MI_ID(hObject,callbackdata) %This Callback stores edited text and sets color to black  
        GuIdata.movieID=get(GuIformat.MI(4),'string'); 
        set(GuIformat.MI(4),'ForegroundColor','black');
    end


   GuIformat.MI(5) = uicontrol(Control,...
    'Units','normalized',...
    'Style','text',...
    'String','Which Movie ID folder to look for?',...
    'Position',[.42 0.8 .2 .1],...
    'HandleVisibility','off');
 
 %Specify Wavelength
  GuIformat.MI(6) = uicontrol(Control,...
    'Units','normalized',...
    'Style','edit',...
    'String','e.g. w01.png, w02.png, w03.png' ,...
    'ForegroundColor',[0.4,0.4,0.4],...
    'Position',[0.72 0.6 .25 .2],...
    'CallBack',@MI_WL);
    function MI_WL(hObject,callbackdata) %This Callback stores edited text and sets color to black  
        GuIdata.FluorWL_group=strsplit(get(GuIformat.MI(6),'string'),', '); 
        set(GuIformat.MI(6),'ForegroundColor','black');
    end


GuIformat.MI(7) = uicontrol(Control,...
    'Units','normalized',...
    'Style','text',...
    'String','Wavelength to process?',...
    'Position',[.72 0.8 .2 .1],...
    'HandleVisibility','off');

%Does Segmentation exist?
 GuIformat.MI(8) = uicontrol(Control,...
    'Units','normalized',...
    'Style','checkbox',...
    'String','Segmentation exists?',...
    'Position',[.2 0.2 .2 .1],...
    'Value', 0,...
    'Callback', @MI_Segment);
    function MI_Segment(hObject,callbackdata) %This Callback will promt user to specifz segmentation folder if ticked 
        if get(GuIformat.MI(8), 'Value')==get(GuIformat.MI(8), 'Max')
          
            %choose Segmentation folder
            choice=questdlg('Specify Segmentation Folder');
            switch choice
                case 'Yes'
                GuIdata.segmentationDirectory=uigetdir();
                set(GuIformat.MI(9),'string',GuIdata.segmentationDirectory);
                set(GuIformat.MI(9),'ForegroundColor','black');
                case 'No'
                dessert = 2;
                case 'Cancel'
                dessert = 0;
            end   
        end
        
        if get(GuIformat.MI(8), 'Value')==get(GuIformat.MI(8), 'Min') %if unticked then clean up
            set(GuIformat.MI(9),'string','No Segmentation Available');
            set(GuIformat.MI(9),'ForegroundColor',[0.4,0.4,0.4]);
            clear GuIdata.segmentationDirectory
        end
        GuIdata.existSegmentation=logical(get(GuIformat.MI(8), 'Value'));%boolean stating if segmentation available or not
   end

  GuIformat.MI(9) = uicontrol(Control,...
    'Units','normalized',...
    'Style','edit',...
    'String','No Segmentation Available' ,...
    'ForegroundColor',[0.4,0.4,0.4],...
    'Position',[.5 0.2 .25 .2]);

end

function StepI

global GuIformat GuIdata;

Control = uipanel('Visible','on',...
    'Position',[0 0.45 .30 .25],...
    'Title','Step I: Generation of flatfield_ap (a base flatfield for all positions)');

%Step1 is about generating flatfield_ap in outputDirectory; if exist, please set runStepI to false
 GuIformat.StepI(1) = uicontrol(Control,...
    'Units','normalized',...
    'Style','checkbox',...
    'String','Generate new flatfield?',...
    'Position',[.0 0.7 .3 .4],...
    'Value', 0,...
    'Callback', @runStepI);
   

            %runAllPositions?
            GuIformat.StepI(2) = uicontrol(Control,...
                'Units','normalized',...
                'Style','checkbox',...
                'String','Run All Positions in Movie',...
                'Position',[.4 0.7 .3 .4],...
                'Value', 0,...
                'Callback', @runAllPositions1);
                

            %create Position Vector
            GuIformat.StepI(3) = uicontrol(Control,...
                'Units','normalized',...
                'Style','edit',...
                'String','e.g. 7, 10 or 7, 1:5',...
                'ForegroundColor',[0.4,0.4,0.4],...
                'Position',[0.0 0.2 .4 .2],...
                'Callback',@positionVectorI);


            GuIformat.StepI(4) = uicontrol(Control,...
                'Units','normalized',...
                'Style','text',...
                'String','Specify Position Vector',...
                'Position',[0 0.4 .4 .2],...
                'HandleVisibility','off');

            %Lambda Parameter
            GuIformat.StepI(5) = uicontrol(Control,...
                'Units','normalized',...
                'Style','edit',...
                'String','between 0 and 5.0, default 2.0',...
                'ForegroundColor',[0.4,0.4,0.4],...
                'Position',[0.6 0.2 .4 .2],...
                'Callback',@lambda1);

            GuIformat.StepI(6) = uicontrol(Control,...
                'Units','normalized',...
                'Style','text',...
                'String','Specify Lambda for Flatfield',...
                'Position',[0.6 0.4 .4 .2],...
                'HandleVisibility','off');

            GuIformat.StepI(7) = uicontrol(Control,...
                'Units','normalized',...
                'Style','pushbutton',...
                'String','?',...
                'ForegroundColor','red',...
                'Position',[0.95 0.3 0.05 .1],...
                'Callback',@lambdahelp);
              
        
        
        %reinitiate panel if unticked to hide input from user
        if get( GuIformat.StepI(1), 'Value') ~= get( GuIformat.StepI(1), 'Max')
            set(GuIformat.StepI(2),'Visible','off');
            set(GuIformat.StepI(3),'Visible','off');
            set(GuIformat.StepI(4),'Visible','off');
            set(GuIformat.StepI(5),'Visible','off');
            set(GuIformat.StepI(6),'Visible','off');
            set(GuIformat.StepI(7),'Visible','off');
        end

         
    

%These functions will only be called if the user generates new flatfile
 function runStepI(hObject,callbackdata)
       
        %check if exist elsewise create flatfield_ap
        GuIdata.outputDirectory = sprintf('%s/%s/Analysis/BaSiC_170315/',GuIdata.TTTDIR,GuIdata.movieID);
        check=exist(fullfile(GuIdata.outputDirectory, 'flatfield_ap'));
        
        if check == 0 %flatfile not there
            GuIdata.runStepI=logical(get( GuIformat.StepI(1), 'Value'));
            if get( GuIformat.StepI(1), 'Value') == get( GuIformat.StepI(1), 'Max')
                msgbox('Will run SLIC to generate new flatfield_ap. Specify remaining parameters!')
             end
        else %flatfile is there
            GuIdata.runStepI=logical(get( GuIformat.StepI(1), 'Value'));
            if get(GuIformat.StepI(1), 'Value') == get( GuIformat.StepI(1), 'Max')
                warndlg('Found flatfield_ap: Running SIC again is not recommended, but if you do specify remaining parameters')
            end
        end
        
        %visualize rest of panel when flatfile has to be created
        if GuIdata.runStepI == get( GuIformat.StepI(1), 'Max')
            set(GuIformat.StepI(2),'Visible','on');
            set(GuIformat.StepI(3),'Visible','on');
            set(GuIformat.StepI(4),'Visible','on');
            set(GuIformat.StepI(5),'Visible','on');
            set(GuIformat.StepI(6),'Visible','on');
            set(GuIformat.StepI(7),'Visible','on');
            
        end
        
           if GuIdata.runStepI == get( GuIformat.StepI(1), 'Min')
            set(GuIformat.StepI(2),'Visible','off');
            set(GuIformat.StepI(3),'Visible','off');
            set(GuIformat.StepI(4),'Visible','off');
            set(GuIformat.StepI(5),'Visible','off');
            set(GuIformat.StepI(6),'Visible','off');
            set(GuIformat.StepI(7),'Visible','off');
           end 
        
 end

    function runAllPositions1(hObject,callbackdata)
        GuIdata.runAllPositions1=logical(get(GuIformat.StepI(2), 'Value'));
        if get(GuIformat.StepI(2), 'Value') == get(GuIformat.StepI(2), 'Min')
            warndlg('Create Position Vector!')
            set(GuIformat.StepI(3),'Visible','on');
            set(GuIformat.StepI(4),'Visible','on');
        end
        
        %hide positional input from user if all positions are being used
        if GuIdata.runAllPositions1 == 1
            set(GuIformat.StepI(3),'Visible','off');
            set(GuIformat.StepI(4),'Visible','off');
        end
        
    end

    function lambdahelp(hObject,callbackdata)  
       %pop up with explanation when ? is pressed
        msgbox('Check the ouptut flatfield_allposition. If not smooth, please increase lambdaFlatfield_all if oversmooth decrease it')
    end

    function lambda1(hObject,callbackdata) %This Callback stores edited text and sets color to black  
        %Get input and convert to numeric
        Input=str2num(get(GuIformat.StepI(5),'string'));%string into cell separated by comma
        GuIdata.lambdaFlatfield_allposition=Input;
        set(GuIformat.StepI(5),'ForegroundColor','black');
    end

    function positionVectorI(hObject,callbackdata) %This Callback stores edited text and sets color to black  
        %Get input and convert to numeric array
        Input=stringconvert(get(GuIformat.StepI(3),'string'));%string into cell separated by comma
        GuIdata.positionVectorI=Input;
        set(GuIformat.StepI(3),'ForegroundColor','black');
    end

end

    
function StepII 

global GuIformat GuIdata;

Control = uipanel('Visible','on',...
    'Position',[0.35 0.45 .3 .25],...
    'Title','Step II: Generation of position-wise flatfield and darkfield');

%generate new position-wise flatfield & darkfield?
 GuIformat.StepII(1) = uicontrol(Control,...
    'Units','normalized',...
    'Style','checkbox',...
    'String','generate new position-wise flat- & darkfield?',...
    'Position',[.0 0.7 .6 .4],...
    'Value', 0,...
    'Callback', @runStep2);

   
            
    %%%%%%%%%the following parameters%%%%%%%%%

    %run all positions?
    GuIformat.StepII(2)=uicontrol(Control,...
        'Units','normalized',...
        'Style','checkbox',...
        'String','Use all positions in movie folder?',...
        'Position',[.6 0.7 .6 .4],...
        'Value', 0,...
        'Callback',@runAllPositions2);
    
    
    %if not all positions are used specify the position vector

    
        GuIformat.StepII(4) = uicontrol(Control,...
            'Units','normalized',...
            'Style','text',...
            'String','Specify Position Vector',...
            'Position',[0 0.6 .4 .2],...
            'HandleVisibility','off');
        
        GuIformat.StepII(3) = uicontrol(Control,...
            'Units','normalized',...
            'Style','edit',...
            'String','e.g. 7, 10 or 7, 1:5',...
            'ForegroundColor',[0.4,0.4,0.4],...
            'Position',[0.02 0.5 .4 .2],...
            'Callback',@positionVectorII);



       GuIformat.StepII(6) = uicontrol(Control,...
            'Units','normalized',...
            'Style','text',...
            'String','Time Range:[Min Max] or leave empty',...
            'Position',[0.6 0.6 .4 .2],...
            'HandleVisibility','off');
        
        
       GuIformat.StepII(5) = uicontrol(Control,...
            'Units','normalized',...
            'Style','edit',...
            'String','Min , Max',...
            'ForegroundColor',[0.4,0.4,0.4],...
            'Position',[0.6 0.5 .4 .2],...
            'Callback',@temporalRangeII);


     
       GuIformat.StepII(7) = uicontrol(Control,...
            'Units','normalized',...
            'Style','checkbox',...
            'String','Estimate Dark-/Flatfield per position',...
            'Position',[0 0.3 .4 .2],...
            'HandleVisibility','off',...
            'Value', 0,...
            'Callback', @useFlatfield_allposition);
       
        GuIformat.StepII(8) = uicontrol(Control,...
            'Units','normalized',...
            'Style','checkbox',...
            'String','Use Segmentation for Dark-/Flatfield computation',...
            'Position',[0.6 0.3 .4 .2],...
            'Callback', @useSegmentation);
        
        GuIformat.StepII(9) = uicontrol(Control,...
            'Units','normalized',...
            'Style','text',...
            'String','Lambda for Darkfield (ADVANCED)',...
            'Position',[0.0 0.1 .4 .2]);
        
        GuIformat.StepII(10) = uicontrol(Control,...
            'Units','normalized',...
            'Style','text',...
            'String','Lambda for Flatfield (ADVANCED)',...
            'Position',[0.6 0.1 .4 .2]);
        
        GuIformat.StepII(11) = uicontrol(Control,...
            'Units','normalized',...
            'Style','edit',...
            'String','e.g. 4, or leave empty',...
            'ForegroundColor',[0.4,0.4,0.4],...
            'Position',[0. 0.0 .4 .2],...
            'Callback', @lambdaFlatfield);
        
        GuIformat.StepII(12) = uicontrol(Control,...
            'Units','normalized',...
            'Style','edit',...
            'String','e.g. 4, or leave empty',...
            'ForegroundColor',[0.4,0.4,0.4],...
            'Position',[0.6 0.0 .4 .2],...
            'Callback', @lambdaDarkfield);
             
        
        %if no flatfield wanted, hide input from user
        if get(GuIformat.StepII(1), 'Value') == get( GuIformat.StepII(1), 'Min')
            set(GuIformat.StepII(2),'Visible','off');
            set(GuIformat.StepII(4),'Visible','off');
            set(GuIformat.StepII(3),'Visible','off');
            set(GuIformat.StepII(5),'Visible','off');
            set(GuIformat.StepII(6),'Visible','off');
            set(GuIformat.StepII(7),'Visible','off');
            set(GuIformat.StepII(8),'Visible','off');
            set(GuIformat.StepII(9),'Visible','off');
            set(GuIformat.StepII(10),'Visible','off');
            set(GuIformat.StepII(11),'Visible','off');
            set(GuIformat.StepII(12),'Visible','off');
        end
    
    %%%%%%%%%the Callback functions of this panel %%%%%%%%%
     function runStep2(hObject,callbackdata)

    %if yes, then specify following parameters
        GuIdata.runStepII=logical(get( GuIformat.StepII(1), 'Value'));
        if get( GuIformat.StepII(1), 'Value') == get( GuIformat.StepII(1), 'Max')
            msgbox('Please specify the following parameters')
        end
        
        %make input visible for user
        if get( GuIformat.StepII(1), 'Value') == get( GuIformat.StepII(1), 'Max')
            set(GuIformat.StepII(2),'Visible','on');
            set(GuIformat.StepII(3),'Visible','on');
            set(GuIformat.StepII(4),'Visible','on');
            set(GuIformat.StepII(5),'Visible','on');
            set(GuIformat.StepII(6),'Visible','on');
            set(GuIformat.StepII(7),'Visible','on');
            set(GuIformat.StepII(8),'Visible','on');
            set(GuIformat.StepII(9),'Visible','on');
            set(GuIformat.StepII(10),'Visible','on');
            set(GuIformat.StepII(11),'Visible','on');
            set(GuIformat.StepII(12),'Visible','on');
        end
        if get( GuIformat.StepII(1), 'Value') == get( GuIformat.StepII(1), 'Min')
            set(GuIformat.StepII(2),'Visible','off');
            set(GuIformat.StepII(3),'Visible','off');
            set(GuIformat.StepII(4),'Visible','off');
            set(GuIformat.StepII(5),'Visible','off');
            set(GuIformat.StepII(6),'Visible','off');
            set(GuIformat.StepII(7),'Visible','off');
            set(GuIformat.StepII(8),'Visible','off');
            set(GuIformat.StepII(9),'Visible','off');
            set(GuIformat.StepII(10),'Visible','off');
            set(GuIformat.StepII(11),'Visible','off');
            set(GuIformat.StepII(12),'Visible','off');
        end
     end  

    function runAllPositions2(hObject,callbackdata)
            GuIdata.runAllPositionsII=logical(get(GuIformat.StepII(2), 'Value'));
            if get(GuIformat.StepII(2), 'Value') == get(GuIformat.StepII(2), 'Min')
                warndlg('Create Position Vector!')
                set(GuIformat.StepII(3),'Visible','on');
                set(GuIformat.StepII(4),'Visible','on');
            end
            if get(GuIformat.StepII(2), 'Value') == get(GuIformat.StepII(2), 'Max')
                set(GuIformat.StepII(3),'Visible','off');
                set(GuIformat.StepII(4),'Visible','off');
            end
        end
    
        function temporalRangeII (hObject,callbackdata)
            string= get(GuIformat.StepII(5), 'String');
            %check if some value was specified and 
            check=isnan(str2double(strsplit(string,', ')));
            if (sum(check) > 0) || isempty(string)
                GuIdata.temporalRangeII = [];
            else 
                GuIdata.temporalRangeII=stringconvert(string);
            end
            
            set(GuIformat.StepII(5),'ForegroundColor','black')
        end
     

        function useFlatfield_allposition (hobject, callbackdata)
            %have to invert user input because user is aksed inverse question
            %to what BaSiC actually wants to know. 
            GuIdata.useFlatfield_allposition= ~logical(get(GuIformat.StepII(7), 'Value'));
        end

    
        function positionVectorII (hobject, callbackdata)
            string=get(GuIformat.StepII(3),'string');
            GuIdata.positionVectorII=stringconvert(string);
            set(GuIformat.StepII(3),'ForegroundColor','black');
        end

        function useSegmentation (hobject, callbackdata)
             GuIdata.useSegmentation= logical(get(GuIformat.StepII(8), 'Value'));
        end
        
        function lambdaDarkfield (hobject, callbackdata)
            string=get(GuIformat.StepII(12),'string');
            GuIdata.lambdaDarkfield= stringconvert(string);
            set(GuIformat.StepII(12),'ForegroundColor','black');
        end

        function lambdaFlatfield (hobject, callbackdata)
            string=get(GuIformat.StepII(11),'string');
            GuIdata.lambdaFlatfield= stringconvert(string);
            set(GuIformat.StepII(11),'ForegroundColor','black');
        end
    
end

function StepIII 

global GuIformat GuIdata;

Control = uipanel('Visible','on',...
    'Position',[0.7 0.45 .3 .25],...
    'Title','Step III: Generation of baseline fluorescence to correct background bleaching');
        
 %generate new basefluor?
 GuIformat.StepIII(1) = uicontrol(Control,...
    'Units','normalized',...
    'Style','checkbox',...
    'String','Generate new basefluor?',...
    'Position',[.0 0.7 .6 .4],...
    'Value', 0,...
    'Callback', @runStep3);

   
            
    %%%%%%%%%the following parameters%%%%%%%%%

    %run all positions?
    GuIformat.StepIII(2)=uicontrol(Control,...
        'Units','normalized',...
        'Style','checkbox',...
        'String','Use all positions in movie folder?',...
        'Position',[.6 0.7 .6 .4],...
        'Value', 0,...
        'Callback',@runAllPositions3);
    
    
    %if not all positions are used specify the position vector
        GuIformat.StepIII(4) = uicontrol(Control,...
            'Units','normalized',...
            'Style','text',...
            'String','Specify Position Vector',...
            'Position',[0 0.6 .4 .2],...
            'HandleVisibility','off');
        
        GuIformat.StepIII(3) = uicontrol(Control,...
            'Units','normalized',...
            'Style','edit',...
            'String','e.g. 7, 10 or 7, 1:5',...
            'ForegroundColor',[0.4,0.4,0.4],...
            'Position',[0.02 0.5 .4 .2],...
            'Callback',@positionVectorIII);



       GuIformat.StepIII(9) = uicontrol(Control,...
            'Units','normalized',...
            'Style','text',...
            'String','Time Range:[Min Max] or leave empty',...
            'Position',[0.6 0.6 .4 .2],...
            'HandleVisibility','off');
        
        
       GuIformat.StepIII(5) = uicontrol(Control,...
            'Units','normalized',...
            'Style','edit',...
            'String','Min , Max',...
            'ForegroundColor',[0.4,0.4,0.4],...
            'Position',[0.6 0.5 .4 .2],...
            'Callback',@temporalRangeIII);

    GuIformat.StepIII(6) = uicontrol(Control,...
            'Units','normalized',...
            'Style','checkbox',...
            'String','Run Correction',...
            'Position',[0 0.2 .4 .2],...
            'HandleVisibility','off',...
            'Callback', @runCorrection);
        
    GuIformat.StepIII(7) = uicontrol(Control,...
            'Units','normalized',...
            'Style','checkbox',...
            'String','use ratioflat in correction',...
            'Position',[0 0.2 .4 .2],...
            'HandleVisibility','off',...
            'Callback', @adjustFlatfield);
        
    GuIformat.StepIII(8) = uicontrol(Control,...
            'Units','normalized',...
            'Style','checkbox',...
            'String',' 8 bit png conversion? (Tif default)',...
            'Position',[0.6 0.2 .4 .2],...
            'HandleVisibility','off',...
            'Callback', @floatOutput);
  
        
        %if no new basefluor wanted, hide input from user
        if get(GuIformat.StepIII(1), 'Value') == get( GuIformat.StepIII(1), 'Min')
            set(GuIformat.StepIII(2),'Visible','off');
            set(GuIformat.StepIII(4),'Visible','off');
            set(GuIformat.StepIII(3),'Visible','off');
            set(GuIformat.StepIII(5),'Visible','off');
            set(GuIformat.StepIII(6),'Visible','off');
            set(GuIformat.StepIII(7),'Visible','off');
            set(GuIformat.StepIII(8),'Visible','off');
            set(GuIformat.StepIII(9),'Visible','off');

        end 
        
        
        %%%%%%%%%%%%Callback Functions%%%%%%%%%%%%%%%%%%%%
        function runStep3(hObject,callbackdata)

        %if yes, then specify following parameters
        GuIdata.runStepII=logical(get( GuIformat.StepII(1), 'Value'));
        if get( GuIformat.StepIII(1), 'Value') == get( GuIformat.StepIII(1), 'Max')
            msgbox('Will run SLIC to generate new flatfield_ap. Specify remaining parameters!')
        end
        
        %make input visible for user
        if get(GuIformat.StepIII(1), 'Value') == get( GuIformat.StepIII(1), 'Max')
            set(GuIformat.StepIII(2),'Visible','on');
            set(GuIformat.StepIII(3),'Visible','on');
            set(GuIformat.StepIII(4),'Visible','on');
            set(GuIformat.StepIII(5),'Visible','on');
            set(GuIformat.StepIII(6),'Visible','on');
            set(GuIformat.StepIII(7),'Visible','on');
            set(GuIformat.StepIII(8),'Visible','on');
            set(GuIformat.StepIII(9),'Visible','off');

        end
        if get( GuIformat.StepIII(1), 'Value') == get( GuIformat.StepII(1), 'Min')
            set(GuIformat.StepIII(2),'Visible','off');
            set(GuIformat.StepIII(3),'Visible','off');
            set(GuIformat.StepIII(4),'Visible','off');
            set(GuIformat.StepIII(5),'Visible','off');
            set(GuIformat.StepIII(6),'Visible','off');
            set(GuIformat.StepIII(7),'Visible','off');
            set(GuIformat.StepIII(8),'Visible','off');
            set(GuIformat.StepIII(9),'Visible','off');

        end
        end  
 
    
      function runAllPositions3(hObject,callbackdata)
            GuIdata.runAllPositionsIII=logical(get(GuIformat.StepIII(2), 'Value'));
            if get(GuIformat.StepIII(2), 'Value') == get(GuIformat.StepIII(2), 'Min')
                warndlg('Create Position Vector!')
                set(GuIformat.StepIII(3),'Visible','on');
                set(GuIformat.StepIII(4),'Visible','on');
            end
            if get(GuIformat.StepIII(2), 'Value') == get(GuIformat.StepIII(2), 'Max')
                set(GuIformat.StepIII(3),'Visible','off');
                set(GuIformat.StepIII(4),'Visible','off');
            end
      end
    
    
      
        function positionVectorIII (hobject, callbackdata)
            string=get(GuIformat.StepIII(3),'string');
            GuIdata.positionVectorIII=stringconvert(string);
            set(GuIformat.StepIII(3),'ForegroundColor','black');
        end
    
    
       function temporalRangeIII (hObject,callbackdata)
            string= get(GuIformat.StepIII(5), 'String');
            %check if some value was specified and 
            check=isnan(str2double(strsplit(string,', ')));
            if (sum(check) > 0) || isempty(string)
                GuIdata.temporalRangeIII = [];
            else 
                GuIdata.temporalRangeIII=stringconvert(string);
            end
            
            set(GuIformat.StepIII(5),'ForegroundColor','black')
        end


        function runCorrection (hobject, callbackdata)
           if get( GuIformat.StepIII(6), 'Value') == get( GuIformat.StepIII(6), 'Max')
                msgbox('Will run SLIC to generate corrected images!')
           end
           GuIdata.runCorrection = logical(get( GuIformat.StepIII(6), 'Value'));
        end
    
        function adjustFlatfield (hobject, callbackdata)
             GuIdata.adjustFlatfield= logical(get(GuIformat.StepIII(7), 'Value'));
        end
    
     
        function floatOutput (hobject, callbackdata)
            %needs to be inverted since user is asked inverse question
             GuIdata.floatOutput= ~(logical(get(GuIformat.StepIII(8), 'Value')));
        end
    
    
    
        
    
end

function StepIV 

global GuIformat GuIdata;

Control = uipanel('Visible','on',...
    'Position',[0.175 0.3 .3 .1],...
    'Title','Step IV: save the specified configuration');


 GuIformat.StepIV(1) = uicontrol(Control,...
            'Units','normalized',...
            'Style','Pushbutton',...
            'String','SAVE CONFIGURATION',...
            'ForegroundColor',[0.4,0.4,0.4],...
            'Position',[0.15 0.25 .7 .5],...
            'Callback',@saveit);
        
    function saveit(hobject, callbackdata)
        %save([outputDirectory '/BaSiCconfig.mat']);
        % movie information
        
        %save('C:\Users\awehling\Desktop\BaSiCconfig.mat','-struct', 'a');
        
    end





end

function StepV 

global GuIformat;

Control = uipanel('Visible','on',...
    'Position',[0.525 0.3 .3 .1],...
    'Title','Step V: run BaSiC_main');

 GuIformat.StepV(1) = uicontrol(Control,...
            'Units','normalized',...
            'Style','Pushbutton',...
            'String','RUN BaSiC',...
            'ForegroundColor',[0.4,0.4,0.4],...
            'Position',[0.15 0.25 .7 .5],...
            'Callback',@Runit);
        
    function Runit(hobject, callbackdata)
    %BaSiC_main([outputDirectory '/BaSiCconfig.mat']);
    end





end


%%Helper Functions

function GUI_Close(hObject,callbackdata)
% Close request function
% to display a question dialog box

global GuIformat


    selection = questdlg('Close The GUI and abort Basig configuration?',...
        'Close Request Function',...
        'Yes','No','Yes');
    switch selection,
        case 'Yes',
            delete(gcf)
        case 'No'
            return
    end

end

function [vector]=stringconvert(string)
%input will be '1,4, 8' or '1:2, 4, 6, 9:14' we want [1 2 4 6 9 10 11 12..]
    a=strsplit(string,', '); %string into cell separated by comma
    numbers=[];
    for i = a
        i = vectorize(i); %cell 2 vector 
        numbers = [numbers,str2num(i)]; %string 2 num and append
    end
    vector=numbers;
end
