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

% Last Modified by GUIDE v2.5 20-Mar-2014 11:30:37

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

%inputrepresentation
%handles.I = varargin{1};
%handles.i = varargin{2};
%handles.U = varargin{3};

global stored_states;
set(gcf,'name',hyreachVersion());
stored_states = cell(0);

handles.output = hObject;
handles.file = [];
handles.x_param = 1;
handles.y_param = 2;

contents = cellstr(get(handles.scenariobox,'String'));
handles.scenario = contents{get(handles.scenariobox,'Value')};


contents = cellstr(get(handles.type,'String'));
handles.option1 = contents{get(handles.type,'Value')};

contents = cellstr(get(handles.algorithm,'String'));
handles.option2 = contents{get(handles.algorithm,'Value')};

contents = cellstr(get(handles.optimizationmethod,'String'));
handles.option3 = contents{get(handles.optimizationmethod,'Value')};

handles.option4 = get(handles.precluster,'Value');
%handles.option5 = get(handles.plotintersectedsets,'Value');
handles.option5 = false;

contents = cellstr(get(handles.eguard,'String'));
handles.option6 = contents{get(handles.eguard,'Value')};

contents = cellstr(get(handles.iguard,'String'));
handles.option7 = contents{get(handles.iguard,'Value')};

handles.directionAngle = pi/2;
handles.importoptions = get(handles.importoptionstag,'Value');

handles.maxRecDepth = str2double(get(handles.edit6,'String'));
handles.timehorizon = str2double(get(handles.edit7,'String'));
handles.timestep = str2double(get(handles.edit8,'String'));

handles.fixpointtimestep = str2double(get(handles.timesteps,'String'));

handles.results = cell(0);
handles.omegas = cell(0);
handles.L = cell(0);
handles.intersectionindices = cell(0);
handles.fixpointindices = cell(0);

handles. plotcount = 1;

handles.option13 = 0.001;

handles.spontaneoustransitions.from = [];
handles.spontaneoustransitions.to = [];
handles.spontaneoustransitions.time = [];

handles.fixpointtolerance_v = 0.001;

handles.Vars = cell(1);
handles.Input = cell(1);
    
% Update handles structure
guidata(hObject, handles);

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


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



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

% TODO: mit Anzahl Dimensionen prüfen
[x,v] = checkIntegerInput(1,Inf,get(hObject,'String'));
if x
    
    handles.x_param = v;
    set(hObject,'String',sprintf('%i',v));
    guidata(hObject,handles);
    
else
    
    msgbox('The input should be an integer between 1 and the number of dimensions !','Invalid Input','error');
    set(hObject,'String',sprintf('%i',handles.x_param));
end;


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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 edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (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 edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double
input = str2double(get(hObject,'String'));

[x,v] = checkIntegerInput(1,Inf,get(hObject,'String'));
if x
    
    handles.y_param = v;
    set(hObject,'String',sprintf('%i',v));
    guidata(hObject,handles);
    
else
    
    msgbox('The input should be an integer between 1 and the number of dimensions !','Invalid Input','error');
    set(hObject,'String',sprintf('%i',handles.y_param));
end;

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


% --- Callback for START Button
function pushbutton1_Callback(hObject, ~, handles)

global stored_states;

% initialize global variables
%stored_states = [];
stored_states = cell(0);    %delete all previously stored states
global trace;
trace = cell(0);

% check if dimensions to plot do not exceed the number of available
% dimensions

[a,xdim] = checkIntegerInput(1,handles.dimensionality,get(handles.edit1,'String'));
if ~a
   msgbox('Plot option X-axis is invalid or exceeds the number of dimensions!','Invalid Input','error');
   error('Plot option X-axis is invalid or exceeds the number of dimensions!');
end;

[a,ydim] = checkIntegerInput(1,handles.dimensionality,get(handles.edit2,'String'));
if ~a
   msgbox('Plot option Y-axis is invalid or exceeds the number of dimensions!','Invalid Input','error');
   error('Plot option Y-axis is invalid or exceeds the number of dimensions!');
end;
    
handles.results = cell(0);
handles.omegas = cell(0);
handles.intersectionindices = cell(0);
handles.fixpointindices = cell(0);
handles.plottime_values = cell(0);
handles.totaltime = cell(0);

guidata(hObject,handles);

if isempty(handles.file)
    msgbox('No input file specified !','Error','error');
else
    
    if get(handles.eraseplot,'Value')
        cla;
    end;
    
    % contains information for interval representation of all intermediate
    % results
    handles.intervals = cell(0);
    %loopcount = 1;
    [handles.results{1},handles.L{1},handles.intersectionindices{1},handles.omegas{1},handles.fixpointindices{1},handles.intervals{1},handles.totaltime{1},handles.plottime_values{1}] = start_analysis(hObject,handles,0);
    
    if get(handles.transition_strategy,'Value') == 5    % nondeterministic analysis -> Depth-First
        while length(stored_states) > 0
            nbr_of_results = length(handles.results);
            
            %loopcount = loopcount +1;
                       
            [handles.results{nbr_of_results+1},handles.L{nbr_of_results+1},handles.intersectionindices{nbr_of_results+1},handles.omegas{nbr_of_results+1},handles.fixpointindices{nbr_of_results+1},handles.intervals{nbr_of_results+1},handles.totaltime{nbr_of_results+1},handles.plottime_values{nbr_of_results+1}] = start_analysis(hObject,handles,length(stored_states));
        end;       
    end;
    
    % handle interval output
    handles.OutputInterval = cell(0);   % contains final intervals in standard interval representation for each dimension
    if get(handles.intervaloutput,'Value')
        % union between intervals of different analysis steps
        interval = -Inf(size(handles.intervals{1}.directions,1),1);
        for i=1:length(handles.intervals)
            interval = max(interval,handles.intervals{i}.values);
        end;
        
        % interval output generation
        for i=1:1:length(interval)/2
            handles.OutputInterval{i}.min = -interval(2*i);
            handles.OutputInterval{i}.max = interval((2*i)-1); 
        end;
        update_interval_output(handles) 
    else
        set(handles.interval_output_list,'String',{''});
    end;
    
    % time outputs:
    sum_totaltime = 0;
    sum_plottime = 0;
    for i=1:length(handles.totaltime)
        sum_totaltime = sum_totaltime + handles.totaltime{i};
        sum_plottime = sum_plottime + handles.plottime_values{i};
    end;
    set(handles.timeelapse,'String',sprintf('%f seconds',sum_totaltime));
    set(handles.plottime_output,'String',sprintf('%f seconds',sum_plottime));
end;
guidata(hObject,handles);

 function update_interval_output(handles)
     if isempty(handles.Vars)
         handles.Vars = cell(1,length(handles.OutputInterval));
         for i=1:length(handles.OutputInterval)
             handles.Vars{i} = sprintf('d%d',i);
         end;
     end;
     
    listcontent = cell(0);
    for i=1:length(handles.OutputInterval)
        listcontent{i} = sprintf('%s: [%f ; %f]\n',handles.Vars{i},handles.OutputInterval{i}.min, handles.OutputInterval{i}.max);
    end;
    
    if length(listcontent) == 0
        listcontent{1}= '';
    end;
    set(handles.interval_output_list,'String',listcontent);

function [results,L,intersectionindices,omegas,fixpointindices,interval_information,totaltime,plottime] = start_analysis(hObject,handles,continue_at_stored_state)
global stored_states;

contents = cellstr(get(handles.type,'String'));
options{1} = contents{get(handles.type,'Value')};
handles.option1 = options{1};

%options{1} = handles.option1;
    options{2} = handles.option2;
    options{3} = handles.option3;
    options{4} = handles.option4;
    options{5} = handles.option5;
    options{6} = handles.option6;
    options{7} = handles.option7;
    options{8} = [handles.x_param; handles.y_param];
    options{9} = handles.importoptions;
    options{10} = handles.file ;
    %options{11} = handles.option11;
    options{11} = [];
    options{12} = handles.option12;
    options{13} = handles.option13;
    
    if strcmp(options{1},'Evaluation matrix')
        options{1} = 'Evaluation';
    elseif strcmp(options{1},'Function representation')
        options{1} = 'Construction';
    end;

    options{16} = [double(handles.spontaneoustransitions.from); double(handles.spontaneoustransitions.to); handles.spontaneoustransitions.time] ; %zusätzliche spontane Transitionen
    
    options{17} = get(handles.fixpoint,'Value');%Fixpunkt detection on ?
    options{18} = handles.fixpointtimestep;
    options{19} = handles.fixpointtolerance_v;
    
    set(handles.figure1, 'pointer', 'watch')
    drawnow;

    if ~isempty(handles.Vars)
        [C,d] = transformExplicitPolyhederInputSet(get(handles.initset,'String'),handles.Vars,true);
        I.polyheder.C = C;
        I.polyheder.d = d;
        options{20} = I;
    
        [C,d] = transformExplicitPolyhederInputSet(get(handles.initset_input,'String'),handles.Input,true);
        if ~isempty(C)
            U.polyheder.C = C;
            U.polyheder.d = d;
        else
            U = [];
        end;
        options{21} = U;
    else
        options{20} = [];
        options{21} = [];
    end;
    options{23} = get(handles.Utype,'Value');
    options{24} = get(handles.transform_invariants,'Value');

    options{25} = get(handles.transition_strategy,'Value');
    
    contents = cellstr(get(handles.calcR,'String'));
    options{26} = contents{get(handles.calcR,'Value')};
    options{27} = get(handles.cut,'Value');
    
    options{30} = get(handles.intervaloutput,'Value');
    %[handles.results{nbr_of_results+1},L,handles.intersectionindices{nbr_of_results+1},handles.omegas{nbr_of_results+1},handles.fixpointindices{nbr_of_results+1}] = hybridReachabilityAnalysis(handles.timehorizon/handles.timestep, handles.timestep ,handles.directionAngle,handles.maxRecDepth,handles.scenario,0,options,continue_at_stored_state);
    [results,L,intersectionindices,omegas,fixpointindices,interval_information,totaltime] = hybridReachabilityAnalysis(handles.timehorizon/handles.timestep, handles.timestep ,handles.directionAngle,handles.maxRecDepth,handles.scenario,0,options,continue_at_stored_state);
    %handles.results{nbr_of_results+1} = x;
    
    set(handles.figure1, 'pointer', 'arrow');
    drawnow;
    
    update_stored_state_list(handles,stored_states);
    
    if size(L,2) - handles.x_param >= 0 && size(L,2) - handles.y_param >= 0
        to = size(results,2);
        if to == 0
            to = length(omegas);
        end;
        
        plottime = 0;
        if get(handles.animated_plot,'Value')
            [~,plottime] = plotanalysisEXT_animated(results,omegas,L,handles.x_param,handles.y_param,1,to,intersectionindices,str2double(get(handles.plotstepsize,'String')),handles.figure,fixpointindices);
        else
            [~,plottime] = plotanalysisEXT(results,omegas,L,handles.x_param,handles.y_param,1,to,intersectionindices,str2double(get(handles.plotstepsize,'String')),handles.figure,fixpointindices);
        end;
        
    else
         msgbox('Plot not possible: One of the specified dimensions to plot exeedes the number of total dimensions.','Invalid Input','error');
    end;
    
    set(handles.newplot,'Enable','on');
    guidata(hObject,handles);

    function update_stored_state_list(handles,stored_states)
        listcontent = cell(0);
    for i=1:length(stored_states)
        listcontent{i} = sprintf('time elapsed in previous locations: %f',stored_states{i}.time);
    end;
    
    if length(listcontent) == 0
        listcontent{1}= '';
    end;
    set(handles.stored_statelist,'String',listcontent);
    
% --- Executes on key press with focus on pushbutton1 and none of its controls.
function pushbutton1_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)




% --- Executes on key press with focus on edit1 and none of its controls.
function edit1_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)



% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over pushbutton1.
function pushbutton1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


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

% Hint: place code in OpeningFcn to populate figure


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%[filename,pathname] = uigetfile({'*.m'},'File Selector');
%handles.file.path = pathname;
%handles.file.name = filename;

%addpath(pathname);
%func = regexprep(filename,'\.(.)*',''); 
%[~,~,~,~,U,~,~,~,~,~,~,~,~,I,~,~,~,~] = feval(func);

%[handles.file.path,handles.file.name,handles.file.type,~,~,~,~,U,~,~,~,~,~,~,~,~,~,I,~,~,~,~,tqq,handles.Vars,handles.Input,~,~] = choseFile([],[]);
[handles.file.path,handles.file.name,handles.file.type,handles.Vars,handles.dimensionality,handles.Input,~,~,~,transitions,~,~,U,~,~] = choseFile([],[]);

% check if a fixpoint triggered transitions was modeled
fpt = false;
for i=1:length(transitions)
    if isfield(transitions{i},'spontaneous')
        if transitions{i}.spontaneous
            if transitions{i}.timeelapse <0
                fpt = true;
            end;
        end;
    end;
end;

if fpt && ~get(handles.fixpoint,'Value')
    msgbox('The model file contains fixpoint triggered spontaneous transitions. The analysis will only properly work if the fixpoint detection is enabled, which is not yet the case!');
end;

if strcmp(handles.file.type,'.m')
    representations = {'box','polyheder','zonotope','unit ball (1)','unit ball (2)','unit ball (oo)'};
    choices = cell(1);

    set(handles.initset_input,'Enable','off');
    set(handles.initset,'Enable','off');
    set(handles.inputrepresentation,'Enable','on');
    set( handles.importoptionstag,'Enable','on');
    set(handles.inputrepresentation,'Value',1);
    count = 1;
    for i=1:length(representations)
        if isfield(U,representations{i})
            choices{count} = representations{i};
            count = count +1;
        end;
    end;
    handles.option12 = choices{1};
    set(handles.inputrepresentation,'String',choices)
    
    set(handles.varlist,'String',[]);
    set(handles.inputlist,'String',[]);
    
elseif strcmp( handles.file.type,'.mdl')
   
    if ~isempty(handles.Input)
        set(handles.initset_input,'Enable','on');
        set(handles.initset_input,'String','');
    else
        set(handles.initset_input,'Enable','off');
    end;
    set(handles.initset,'Enable','on');
    set(handles.inputrepresentation,'Enable','off');
    set( handles.importoptionstag,'Enable','off');
    set( handles.edit7,'Enable','on');
    set( handles.edit8,'Enable','on');
    set( handles.importoptionstag,'Value',0);
    handles.importoptions = get(handles.importoptionstag,'Value');
    handles.option12 = []; % kein input set in Datei vorhanden
    
    set(handles.varlist,'String',handles.Vars);
    set(handles.inputlist,'String',handles.Input);
end;

set(handles.loaded,'String',handles.file.name);

guidata(hObject,handles);


% --- Executes on selection change in algorithm.
function algorithm_Callback(hObject, eventdata, handles)
% hObject    handle to algorithm (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns algorithm contents as cell array
%        contents{get(hObject,'Value')} returns selected item from algorithm
contents = cellstr(get(hObject,'String'));
handles.option2 = contents{get(hObject,'Value')};
guidata(hObject,handles);

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

% Hint: popupmenu 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 box.
function box_Callback(hObject, eventdata, handles)
% hObject    handle to box (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of box


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

% Hint: get(hObject,'Value') returns toggle state of oct


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

% Hint: get(hObject,'Value') returns toggle state of user



function angle_Callback(hObject, eventdata, handles)
% hObject    handle to angle (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 angle as text
%        str2double(get(hObject,'String')) returns contents of angle as a double
[x,v] = checkDoubleInput(2,Inf,get(hObject,'String'));
if x
    handles.directionAngle = pi/v;
    guidata(hObject,handles);
else
    msgbox('The angle for the rotation should be in (0;pi/2]. Therefore, the denominator should be greater or equal to 2 !','Invalid Input','error');
end;


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

% Hints: contents = cellstr(get(hObject,'String')) returns scenariobox contents as cell array
%        contents{get(hObject,'Value')} returns selected item from scenariobox
contents = cellstr(get(hObject,'String'));

% enable transition field
set(handles.edit6,'Enable','on');

choices{1} = 'Evaluation matrix';
if ~strcmp(contents{get(hObject,'Value')},'NoScale')
    
    if ~(strcmp(contents{get(hObject,'Value')},'AlgoInv') || strcmp(contents{get(hObject,'Value')},'AlgoInv2'))
        choices{2} = 'Function representation';
    end;

    set(handles.calcR,'Visible','off');
    set(handles.calcRtext,'Visible','off');
    set(handles.Utype,'Visible','on');
    
    if strcmp(contents{get(hObject,'Value')},'ConstU')
        %constU
        choices2{1} = 'ConstU initial over-approximation';
        choices2{2} = 'SpaceEx initial over-approximation';
        choices2{3} = 'PreciseOmega0 initial over-approximation';
        set(handles.Utype,'String',choices2);

    else 
        %spaceEx || AlgoInv || AlgoInv2
        choices2{1} = 'SpaceEx initial over-approximation';
        choices2{2} = 'PreciseOmega0 initial over-approximation';
        set(handles.Utype,'String',choices2);
    end;
else
    set(handles.Utype,'Visible','off');
end;

set(handles.type,'Value',1);
set(handles.precluster,'Enable','on');
set(handles.Utype,'Value',1);
set(handles.type,'String',choices);

handles.scenario = contents{get(hObject,'Value')};

guidata(hObject,handles);


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

% Hint: popupmenu 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 precluster.
function precluster_Callback(hObject, eventdata, handles)
% hObject    handle to precluster (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of precluster
handles.option4 = get(hObject,'Value');
guidata(hObject,handles);


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

% Hint: get(hObject,'Value') returns toggle state of checkbox2


% --- Executes on selection change in type.
function type_Callback(hObject, eventdata, handles)
% hObject    handle to type (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns type contents as cell array
%        contents{get(hObject,'Value')} returns selected item from type
contents = cellstr(get(hObject,'String'));
handles.option1 = contents{get(hObject,'Value')};

if get(hObject,'Value') == 2
    if get(handles.fixpoint,'Value')
        msgbox('Fixpoint detection cannot be used with flowpipe construction type "Function representation"!');
        set(handles.fixpoint,'Value',0);
    end;
    set(handles.fixpoint,'Enable','off');
    
    % no preclustering for function handles
    handles.stored_precluster_value = get(handles.precluster,'Value');
    set(handles.precluster,'Value',0);
    set(handles.precluster,'Enable','off');
    
    % no transition handling for function handles
    set(handles.edit6,'String','1');
    set(handles.edit6,'Enable','off');
else
    set(handles.precluster,'Value',handles.stored_precluster_value);
    set(handles.fixpoint,'Enable','on');
    set(handles.precluster,'Enable','on');
    
    set(handles.edit6,'Enable','on');
end;

guidata(hObject,handles);


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

% Hint: popupmenu 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 selection change in eguard.
function eguard_Callback(hObject, eventdata, handles)
% hObject    handle to eguard (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns eguard contents as cell array
%        contents{get(hObject,'Value')} returns selected item from eguard
contents = cellstr(get(hObject,'String'));
handles.option6 = contents{get(hObject,'Value')};
guidata(hObject,handles);


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

% Hint: popupmenu 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 selection change in iguard.
function iguard_Callback(hObject, eventdata, handles)
% hObject    handle to iguard (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns iguard contents as cell array
%        contents{get(hObject,'Value')} returns selected item from iguard
contents = cellstr(get(hObject,'String'));
handles.option7 = contents{get(hObject,'Value')};
guidata(hObject,handles);

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

% Hint: popupmenu 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 selection change in optimizationmethod.
function optimizationmethod_Callback(hObject, eventdata, handles)
% hObject    handle to optimizationmethod (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns optimizationmethod contents as cell array
%        contents{get(hObject,'Value')} returns selected item from optimizationmethod
contents = cellstr(get(hObject,'String'));
handles.option3 = contents{get(hObject,'Value')};


 
if strcmp(handles.option3,'linprog')
    
    set(handles.algorithm,'Enable','on');
    set(handles.algorithm,'Value',2.0);
    choices{1} = 'LargeScale';
    choices{2} = 'MediumScale';
    choices{3} = 'Simplex';
 
elseif strcmp(handles.option3,'fmincon')
    
    set(handles.algorithm,'Enable','on');
    choices{1} = 'active-set';
    choices{2} = 'interior-point';
    choices{3} = 'trust-region-reflective';
    choices{4} = 'sqp';
    set(handles.algorithm,'Value',1.0);
    
elseif strcmp(handles.option3,'cvx')
    choices{1} = ' ';
    set(handles.algorithm,'Value',1.0);
    set(handles.algorithm,'Enable','off');
    
elseif strcmp(handles.option3,'mpt')
    choices{1} = 'GLPK';
    choices{2} = 'CDD Criss-Cross';
    choices{3} = 'CDD Dual Simplex';
    choices{4} = 'SeDuMi';
    
    set(handles.algorithm,'Value',1.0);
    set(handles.algorithm,'Enable','on');
    
end;

set(handles.algorithm,'String',choices)

contents = cellstr(get(handles.algorithm,'String'));
handles.option2 = contents{get(handles.algorithm,'Value')};


guidata(hObject,handles);

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

% Hint: popupmenu 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 plotintersectedsets.
function plotintersectedsets_Callback(hObject, eventdata, handles)
% hObject    handle to plotintersectedsets (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of plotintersectedsets
handles.option5 = get(hObject,'Value');

guidata(hObject,handles);


% --- Executes when selected object is changed in uipanel5.
function uipanel5_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uipanel5 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)
if hObject == handles.box
    handles.directionAngle = pi/2;
    set(handles.angle,'Enable','off');
elseif hObject == handles.oct
    handles.directionAngle = pi/4;
    set(handles.angle,'Enable','off');
elseif hObject == handles.user
    set(handles.angle,'Enable','on');
    handles.directionAngle = get(handles.angle,'Value');
end;

guidata(hObject,handles);



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

[x,v] = checkIntegerInput(1,Inf,get(hObject,'String'));
if x
    
    handles.maxRecDepth = v ;
    set(handles.edit6,'String',{sprintf('%i',v)});
    guidata(hObject,handles);
    
else
    
    msgbox('The Maximal number of iterations should be an integer greater or equal 1 !','Invalid Input','error');
    set(hObject,'String',sprintf('%i',handles.maxRecDepth));
end;

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

if checkDoubleInput(0,Inf,get(hObject,'String'))
    handles.timehorizon = str2double(get(hObject,'String'));
    guidata(hObject,handles);
else
    msgbox('The Local time horizon should be a double value greater 0 !','Invalid Input','error');
    set(hObject,'String',sprintf('%i',handles.timehorizon));
end;


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

if checkDoubleInput(0+eps,Inf,get(hObject,'String'))
    handles.timestep = str2double(get(hObject,'String'));
    guidata(hObject,handles);
else
    msgbox('The timestep has to be a double value greater 0 !','Invalid Input','error');
    set(hObject,'String',sprintf('%i',handles.timestep));
end;


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

% Hint: get(hObject,'Value') returns toggle state of importoptionstag
if get(hObject,'Value')
    set(handles.edit7,'Enable','off');
    set(handles.edit8,'Enable','off');
else
    set(handles.edit7,'Enable','on');
    set(handles.edit8,'Enable','on');
end;

handles.importoptions = get(hObject,'Value');
guidata(hObject,handles);


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

% Hint: get(hObject,'Value') returns toggle state of eraseplot


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

% Input Dialog
prompt = {'Enter file name'};
dlg_title = 'Export plot';
num_lines = 1;
def = {'20','hsv'};
answer = inputdlg(prompt,dlg_title,num_lines,def);

if ~isempty(answer)
    % Figur anlegen und manipulieren
     h = figure(2);
    figurehandle = copyobj(handles.figure,h);
 
    pos = get(figurehandle,'Position');
    pos(1) = 13;
    pos(2) = 8;
    set(figurehandle,'Position',pos);
  
    set(h,'Position',[100 100 pos(3)*6 pos(4)*17])
    
    content = get(handles.format,'String');
    format = content{get(handles.format,'Value')};
    
    % speichern
    saveas(h,answer{1},format);
    close(h);
end;

% --- Executes on selection change in initialrepresentation.
function initialrepresentation_Callback(hObject, eventdata, handles)
% hObject    handle to initialrepresentation (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns initialrepresentation contents as cell array
%        contents{get(hObject,'Value')} returns selected item from initialrepresentation
contents = cellstr(get(hObject,'String'));
handles.option11 = contents{get(hObject,'Value')};
guidata(hObject,handles);

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

% Hint: popupmenu 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 selection change in inputrepresentation.
function inputrepresentation_Callback(hObject, eventdata, handles)
% hObject    handle to inputrepresentation (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns inputrepresentation contents as cell array
%        contents{get(hObject,'Value')} returns selected item from inputrepresentation
contents = cellstr(get(hObject,'String'));
handles.option12 = contents{get(hObject,'Value')};
guidata(hObject,handles);

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

% Hint: popupmenu 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 when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
delete(hObject);


% --- Executes on selection change in format.
function format_Callback(hObject, eventdata, handles)
% hObject    handle to format (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns format contents as cell array
%        contents{get(hObject,'Value')} returns selected item from format


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

% Hint: popupmenu 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 newplot.
function newplot_Callback(hObject, eventdata, handles)
% hObject    handle to newplot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
   

figure;
newhandle= axes;

for i=1:length(handles.results)
    if size(handles.L{i},2) - handles.x_param >= 0 && size(handles.L{i},2) - handles.y_param >= 0
        to = size(handles.results{i},2);
        if to == 0
            to = length(handles.omegas{i});
        end;

        if get(handles.animated_plot,'Value')
            plotanalysisEXT_animated(handles.results{i},handles.omegas{i},handles.L{i},handles.x_param,handles.y_param,1,to,handles.intersectionindices{i},str2double(get(handles.plotstepsize,'String')),newhandle,handles.fixpointindices{i});
        else
            plotanalysisEXT(handles.results{i},[],handles.L{i},handles.x_param,handles.y_param,1,size(handles.results{i},2),handles.intersectionindices{i},str2double(get(handles.plotstepsize,'String')),newhandle,handles.fixpointindices{i});
        end;
    else
        msgbox('Plot not possible: One of the specified dimensions to plot exeedes the number of total dimensions.','Invalid Input','error');
    end;
end;

guidata(hObject,handles);



function scale_Callback(hObject, eventdata, handles)
% hObject    handle to scale (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 scale as text
%        str2double(get(hObject,'String')) returns contents of scale as a double
[x,v] = checkDoubleInput(0,1,get(hObject,'String'));

if x
    handles.option13 = v;
else
    msgbox('The input should be an integer between 0 and 1 !','Invalid Input','error');
    set(hObject,'String',handles.option13);
end;

guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function scale_CreateFcn(hObject, eventdata, handles)
% hObject    handle to scale (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 pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
init_additionalpkgs({'mpt','cvx'});


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

% Hint: get(hObject,'Value') returns toggle state of checkbox6
if get(hObject,'Value')
    
    set(handles.threads,'Enable','on');
    [x,v] = checkIntegerInput(1,1000,get(handles.threads,'String'));
    if x
        set(handles.figure1, 'pointer', 'watch')
        drawnow;
        matlabpool('local', v);
        handles.multithreading = true;
        set(handles.figure1, 'pointer', 'arrow');
        drawnow;
    else
        handles.multithreading = false;
    end;
    
else
    set(handles.threads,'Enable','off');
    matlabpool('close');
    handles.multithreading = false;
end;
guidata(hObject,handles);

function threads_Callback(hObject, eventdata, handles)
% hObject    handle to threads (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 threads as text
%        str2double(get(hObject,'String')) returns contents of threads as a double
[x,v] = checkIntegerInput(1,1000,get(hObject,'String'));
if handles.multithreading && x
    matlabpool('close');
    matlabpool('local',v);
elseif ~handles.multithreading && x
    set(handles.figure1, 'pointer', 'watch')
    drawnow;
    matlabpool('local',v);
    set(handles.figure1, 'pointer', 'arrow');
    drawnow;
    handles.multithreading = true;
else
    msgbox('The input should be an integer between 1 and 1000 !','Invalid Input','error');
    matlabpool('close');
    set(hObject,'Enable','off');
    set(hObject,'String','2');
    set(handles.checkbox6,'Value',0);
end;
guidata(hObject,handles);

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

% Hints: contents = cellstr(get(hObject,'String')) returns listbox2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox2


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

% Hint: listbox 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 from_Callback(hObject, eventdata, handles)
% hObject    handle to from (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 from as text
%        str2double(get(hObject,'String')) returns contents of from as a double


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


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


% --- Executes during object creation, after setting all properties.
function time_CreateFcn(hObject, eventdata, handles)
% hObject    handle to time (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 pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[fromx,fromv] = checkIntegerInput(0,Inf,get(handles.from,'String'));
[tox,tov] = checkIntegerInput(0,Inf,get(handles.to,'String'));
[timex,timev] = checkDoubleInput(0,Inf,get(handles.time,'String'));

if fromx && tox && timex
    handles.spontaneoustransitions.from = [handles.spontaneoustransitions.from fromv];
    handles.spontaneoustransitions.to = [handles.spontaneoustransitions.to tov];
    handles.spontaneoustransitions.time = [handles.spontaneoustransitions.time timev];
    
    list = get(handles.transitionlist,'String');
    if ~isempty(list)
        list{length(list)+1} = sprintf('from: %d to: %d at: %f',fromv,tov,timev);
    else
        list{1} = sprintf('from: %d to: %d at: %f',fromv,tov,timev);
    end;
    
    set(handles.transitionlist,'String',list);
    
    set(handles.st_nbr,'String',sprintf('%d',length(handles.spontaneoustransitions.from)));
else
    % Reset der Eingabe
    set(handles.spontaneoustransitions.from,'String','1');
    set(handles.spontaneoustransitions.to,'String','1');
    set(handles.spontaneoustransitions.time,'String','0.5');
    
    error('Wrong input for spontaneous transitions'); 
end;
guidata(hObject,handles);

% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.spontaneoustransitions.from = [];
handles.spontaneoustransitions.to = [];
handles.spontaneoustransitions.time = [];

set(handles.st_nbr,'String','0');
set(handles.transitionlist,'String',{'please chose a transition','empty'});
guidata(hObject,handles);


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

% Hint: get(hObject,'Value') returns toggle state of fixpoint
if get(hObject,'Value')
    type = get(handles.type,'String');
    if ischar(type) ||  ~strcmp(type{get(handles.type,'Value')},'Function representation')
        set(handles.timesteps,'Enable','on');
        %set(handles.transitionlist,'Enable','on');
        %set(handles.link,'Enable','on');
        set(handles.fixpointtolerance,'Enable','on');
    else
        msgbox('Fixpoint detection cannot be used with flowpipe construction type "Function representation"!');
    end;
else
    set(handles.timesteps,'Enable','off');
    %set(handles.transitionlist,'Enable','off');
    %set(handles.link,'Enable','off');
    set(handles.fixpointtolerance,'Enable','off');
end;
guidata(hObject,handles);


function timesteps_Callback(hObject, eventdata, handles)
% hObject    handle to timesteps (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 timesteps as text
%        str2double(get(hObject,'String')) returns contents of timesteps as a double
[x,v] = checkIntegerInput(1,Inf,get(handles.fixpointtimestep,'String'));


if x
    handles.fixpointtimestep = v;
else
    handles.fixpointtimestep = -1;
    error('Wrong input for spontaneous transitions');
end;
guidata(hObject,handles);

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

% Hints: contents = cellstr(get(hObject,'String')) returns transitionlist contents as cell array
%        contents{get(hObject,'Value')} returns selected item from transitionlist


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

% Hint: popupmenu 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 selection change in listbox3.
function listbox3_Callback(hObject, eventdata, handles)
% hObject    handle to listbox3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns listbox3 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox3


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

% Hint: listbox 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 link.
function link_Callback(hObject, eventdata, handles)
% hObject    handle to link (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Wähle spontane Transitionen und deklariere sie als Fixpunktabhängig
%contents = cellstr(get(handles.transitionlist,'String'));
%contents{get(handles.transitionlist,'Value')};
transition = get(handles.transitionlist,'Value')-2;
handles.spontaneoustransitions.time(transition) = -1;

if transition > 0
    list = get(handles.transitionlist,'String');
    if ~isempty(list)
        x = sscanf(list{transition+2},'%*s %f');
        list{transition+2} = sprintf('from: %d to: %d at: fixpoint',x(1),x(2));
    end;
    
    set(handles.transitionlist,'String',list);
else
end;
    
guidata(hObject,handles);



function fixpointtolerance_Callback(hObject, eventdata, handles)
% hObject    handle to fixpointtolerance (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 fixpointtolerance as text
%        str2double(get(hObject,'String')) returns contents of fixpointtolerance as a double
[x,v] = checkDoubleInput(0,Inf,get(handles.fixpointtolerance,'String'));

if x
    handles.fixpointtolerance_v = v;
    
else
    % Reset der Eingabe
    handles.fixpointtolerance_v = 0.001;
    set(handles.fixpointtolerance,'String','0.001');
    msgbox('The input should be a positive double value !','Invalid Input','error');
end;
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function fixpointtolerance_CreateFcn(hObject, eventdata, handles)
% hObject    handle to fixpointtolerance (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 plotstepsize_Callback(hObject, eventdata, handles)
% hObject    handle to plotstepsize (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 plotstepsize as text
%        str2double(get(hObject,'String')) returns contents of plotstepsize as a double
[x,v] = checkIntegerInput(1,Inf,get(handles.plotstepsize,'String'));

if x
    
else
    msgbox('The input should be an integer greater 1 !','Invalid Input','error');
    set(hObject,'String','1');
end;

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

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


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

 contents = cellstr(get(hObject,'String')); % returns Utype contents as cell array
 selection = contents{get(hObject,'Value')}; % returns selected item from Utype


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

% Hint: popupmenu 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 figure1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes on selection change in varlist.
function varlist_Callback(hObject, eventdata, handles)
% hObject    handle to varlist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns varlist contents as cell array
%        contents{get(hObject,'Value')} returns selected item from varlist


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

% Hint: listbox 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 selection change in inputlist.
function inputlist_Callback(hObject, eventdata, handles)
% hObject    handle to inputlist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns inputlist contents as cell array
%        contents{get(hObject,'Value')} returns selected item from inputlist


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

% Hint: listbox 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 animated_plot.
function animated_plot_Callback(hObject, eventdata, handles)
% hObject    handle to animated_plot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of animated_plot


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

% Hint: get(hObject,'Value') returns toggle state of transform_invariants


% --- Executes on selection change in stored_statelist.
function stored_statelist_Callback(hObject, eventdata, handles)
% hObject    handle to stored_statelist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns stored_statelist contents as cell array
%        contents{get(hObject,'Value')} returns selected item from stored_statelist


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

% Hint: listbox 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 load_and_start.
function load_and_start_Callback(hObject, eventdata, handles)
% hObject    handle to load_and_start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global stored_states;

p = get(handles.stored_statelist,'Value');
%results = stored_states{p}.results;
%iteration = stored_states{p}.iteration;
%time = stored_states{p}.time;
%omegas = stored_states{p}.omegas;
if length(stored_states) > 0
    start_analysis(hObject,handles,p);
end;


% --- Executes on button press in delete_state_button.
function delete_state_button_Callback(hObject, eventdata, handles)
% hObject    handle to delete_state_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global stored_states;
delete_state(get(handles.stored_statelist,'Value'));

update_stored_state_list(handles,stored_states);


% --- Executes on selection change in transition_strategy.
function transition_strategy_Callback(hObject, eventdata, handles)
% hObject    handle to transition_strategy (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns transition_strategy contents as cell array
%        contents{get(hObject,'Value')} returns selected item from transition_strategy


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

% Hint: popupmenu 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 selection change in calcRtext.
function calcR_Callback(hObject, eventdata, handles)
% hObject    handle to calcRtext (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns calcRtext contents as cell array
%        contents{get(hObject,'Value')} returns selected item from calcRtext


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

% Hint: popupmenu 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 calcR_CreateFcn(hObject, eventdata, handles)
% hObject    handle to calcR (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu 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 cut.
function cut_Callback(hObject, eventdata, handles)
% hObject    handle to cut (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of cut


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

% Hint: get(hObject,'Value') returns toggle state of intervaloutput


% --- Executes on selection change in interval_output_list.
function interval_output_list_Callback(hObject, eventdata, handles)
% hObject    handle to interval_output_list (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns interval_output_list contents as cell array
%        contents{get(hObject,'Value')} returns selected item from interval_output_list


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

% Hint: listbox 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
