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

% Last Modified by GUIDE v2.5 11-Jan-2019 11:35:34

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

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

% Update handles structure
guidata(hObject, handles);
set(handles.text9,'BackgroundColor',[0.85 0.2 0.2]);
set(handles.text10,'BackgroundColor',[0.85 0.2 0.2]);
set(handles.text11,'BackgroundColor',[0.85 0.2 0.2]);
set(handles.text12,'BackgroundColor',[0.85 0.2 0.2]);
set(handles.text13,'BackgroundColor',[0.85 0.2 0.2]);
set(handles.text27,'BackgroundColor',[0.85 0.2 0.2]);
set(handles.text26,'BackgroundColor',[0.85 0.2 0.2]);
set(handles.text18,'BackgroundColor','blue');

DataT1={ 0.03  77.1  79;
         0.94 66.82 68;
         0.03 56.54 56;
         0.0  46.24 38};
  
DataT2={4 5 6 6;
        3 3 5 5;
        3 3 3 3;
        3 3 3 3};
  
DataT3={'' '' '' '';
        '' '' '' '';
        '' '' '' '';
        '' '' '' ''};

DataT4={'' '' '' '';
        '' '' '' '';
        '' '' '' '';
        '' '' '' ''};
    

  
  set(handles.uitable1,'Data',DataT1);
  set(handles.uitable2,'Data',DataT2);
  set(handles.uitable3,'Data',DataT3);
  set(handles.uitable4,'Data',DataT4);
  
CalcAll(handles); 

function varargout = untitled_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 r_Callback(hObject, eventdata, handles)
Value=get(hObject,'String');
if isnan(str2double(Value))
    set(handles.text9,'BackgroundColor',[0.85 0.2 0.2]);
else
    set(handles.text9,'BackgroundColor','blue');
end


function sij_Callback(hObject, eventdata, handles)
Value=get(hObject,'String');
if isnan(str2double(Value))
    set(handles.text10,'BackgroundColor',[0.85 0.2 0.2]);
else
    set(handles.text10,'BackgroundColor','blue');
end

function vi_Callback(hObject, eventdata, handles)
Value=get(hObject,'String');
if isnan(str2double(Value))
    set(handles.text11,'BackgroundColor',[0.85 0.2 0.2]);
else
    set(handles.text11,'BackgroundColor','blue');
end


function vj_Callback(hObject, eventdata, handles)
Value=get(hObject,'String');
if isnan(str2double(Value))
    set(handles.text12,'BackgroundColor',[0.85 0.2 0.2]);
else
    set(handles.text12,'BackgroundColor','blue');
end


function oi_Callback(hObject, eventdata, handles)
Value=get(hObject,'String');
if isnan(str2double(Value))
    set(handles.text13,'BackgroundColor',[0.85 0.2 0.2]);
else
    set(handles.text13,'BackgroundColor','blue');
end



function B_Callback(hObject, eventdata, handles)

Value=get(hObject,'String');
if isnan(str2double(Value))
    set(handles.text18,'BackgroundColor',[0.85 0.2 0.2]);
else
    set(handles.text18,'BackgroundColor','blue');
end



% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(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)
r=str2double(get(handles.r,'String'));
sij=str2double(get(handles.sij,'String'));
vi=str2double(get(handles.vi,'String'));
vj=str2double(get(handles.vj,'String'));
oi=str2double(get(handles.oi,'String'));
b=str2double(get(handles.B,'String'));

if isnan(vi) || isnan(vj) || isnan(r) || isnan(oi) ||...
        isnan(sij) || isnan(b)
    
    disp('Wrong')
    set(handles.T,'String','Cant perform calculation');
    set(handles.Tall,'String','tij = ???') 
    errordlg('Vše musí být zadané','Error','modal');
    
else
       
     disp('OK')
    if vi<=vj
        Tij=max([(sij/vj);oi]);
        display((sij/vj));
    else
        Tij=max([((r+sij)/vj)-(r/vi);oi]);
    end
    set(handles.T,'String',['Tij + B = ', num2str(Tij),' + ', num2str(b)])   
    set(handles.Tall,'String',['tij = ', num2str(Tij+b)]) 
   Value=get(hObject,'String');
end




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

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,...
        'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

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

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,...
        'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

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

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,...
        'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

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

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,...
        'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

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

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,...
        'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


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

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,...
        'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes when entered data in editable cell(s) in uitable1.
function uitable1_CellEditCallback(hObject, eventdata, handles)
CalcAll(handles);


    




% --- Executes when entered data in editable cell(s) in uitable2.
function uitable2_CellEditCallback(hObject, eventdata, handles)

DataT2=get(handles.uitable2,'Data');
DataT2(~cellfun(@isnumeric,DataT2(:))) = {NaN};
DataT2=cell2mat(DataT2).*1852;

display(DataT2)

% --- Executes when entered data in editable cell(s) in uitable3.
function uitable3_CellEditCallback(hObject, eventdata, handles)


% --- Executes when entered data in editable cell(s) in uitable4.
function uitable4_CellEditCallback(hObject, eventdata, handles)



function editNM_Callback(hObject, eventdata, handles)

number=str2double(get(hObject,'String'))*1852;
set(handles.text20,'String',['NM is ', num2str(number),' m']);
Value=get(hObject,'String');
if isnan(str2double(Value))
    set(handles.text26,'BackgroundColor',[0.85 0.2 0.2]);
else
    set(handles.text26,'BackgroundColor','blue');
end
% --- Executes during object creation, after setting all properties.


function editNM_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editNM (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 editM_Callback(hObject, eventdata, handles)
number=str2double(get(hObject,'String'))/1852;
set(handles.text21,'String',['m is ', num2str(number),' NM']);
Value=get(hObject,'String');
if isnan(str2double(Value))
    set(handles.text27,'BackgroundColor',[0.85 0.2 0.2]);
else
    set(handles.text27,'BackgroundColor','blue');
end


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

DataT1=get(handles.uitable1,'Data');
DataT1(~cellfun(@isnumeric,DataT1(:))) = {NaN};
DataT1=cell2mat(DataT1);

DataT2=get(handles.uitable2,'Data');
DataT2(~cellfun(@isnumeric,DataT2(:))) = {NaN};
DataT2=cell2mat(DataT2).*1852;

display(DataT2)

Tij=[];
pij=[];


for i=1:4
 for j=1:4

          
    vi=DataT1(i,2);
    vj=DataT1(j,2);
    
    
    if vi<=vj
        Tij(i,j)=max([(DataT2(i,j)/vj); DataT1(i,3)]);
    else
        Tij(i,j)=max([((9260+DataT2(i,j))/vj)-(9260/vi); DataT1(i,3)]);
    end
    
    pij(i,j)=DataT1(i,1)*DataT1(j,1);
    
 end
end

set(handles.uitable3,'Data',Tij);

if get(handles.checkbox2,'Value')==1
set(handles.uitable4,'Data',pij.*Tij);
else
set(handles.uitable4,'Data',pij);
end

display(['hello ', num2str(sum(sum(pij.*(Tij+10))))])

set(handles.textE,'String', ['E[Tij] = ', num2str(sum(sum(pij.*(Tij+10)))),...
    ' s']);
set(handles.textn,'String', ['n = ',...
    num2str( (3600/( sum(sum(pij.*(Tij+10))))))]);


% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
CalcAll(handles);


% --- Executes on button press in Save.
function Save_Callback(hObject, eventdata, handles)



filter = {'*.xls';'*.xlsx'};

[file, path] = uiputfile(filter);


try
 
    


 
  if file==0
    
    
  else
      DataT1 = get(handles.uitable1,'Data');
      DataT2 = get(handles.uitable2,'Data');
      DataT3 = get(handles.uitable3,'Data');
      DataT4 = get(handles.uitable4,'Data');

      Tabs(1,:)={'' '' '' '' ''};
      Tabs(1,3:end)=get(handles.uitable1,'ColumnName');
      Tabs(2:5,1)=get(handles.uitable1,'RowName');
      Tabs(2:5,3:5)=DataT1;
      Tabs(6,:)={'' '' '' '' ''};
      Tabs(7,:)={'' '' '' '' ''};
      Tabs(7,2:end)=get(handles.uitable2,'ColumnName');
      Tabs(7,1)={'Sij'};
      Tabs(8:11,1)=get(handles.uitable2,'RowName');
      Tabs(8:11,2:end)=DataT2;
      Tabs(12,:)={'' '' '' '' ''};
      Tabs(13,:)={'' '' '' '' ''};
      Tabs(13,1)={'Tij'};
      Tabs(13,2:end)=get(handles.uitable3,'ColumnName');
      Tabs(14:17,1)=get(handles.uitable3,'RowName');
      Tabs(14:17,2:end)=num2cell(DataT3);
      Tabs(18,:)={'' '' '' '' ''};
      Tabs(19,:)={'' '' '' '' ''};
      Tabs(19,1)={'Pij'};
      Tabs(19,2:end)=get(handles.uitable4,'ColumnName');
      Tabs(20:23,1)=get(handles.uitable4,'RowName');
      Tabs(20:23,2:end)=num2cell(DataT4);
      Tabs(24,:)={'' '' '' '' ''};
      Tabs(25,:)={'' '' '' '' ''};
      Tabs(26,1)={get(handles.textE,'String')};
      Tabs(27,1)={get(handles.textn,'String')};
      
      display(Tabs)
      xlswrite([path,file],Tabs);
      end

  
catch Error
   errordlg('Something is wrong','Error - Saving problem','modal');
end
