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

% Last Modified by GUIDE v2.5 25-Apr-2016 17:14:05

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

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

% Update handles structure
guidata(hObject, handles);

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


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


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

handles.fileName = uigetfile('*.xls') % příkaz pro vyhledání xls souboru
guidata(hObject, handles)

setListboxString(handles.listboxTemperature, eventdata, handles)%vypsání názvů proměnných
setListboxString(handles.listboxExposure, eventdata, handles)
setListboxString(handles.listboxVykon, eventdata, handles)

function setListboxString(hObject, eventdata, handles)
fileName = handles.fileName;
[numbers, colNames] = xlsread(fileName); %vypíše čísla a názvy proměnných

colNames=colNames(1,:);
set(hObject, 'String', colNames);


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

a=str2double(get(handles.a,'String')); %získání zadaného koeficientu a
a=a/1600; %převod z % na desetinné číslo
b=str2double(get(handles.b,'String')); %získání zadaného koeficientu b
b=(a*1600-(a*1600-(b*0.01*a*1600)))/1600; %převod z %/rok na desetinné číslo
d=str2double(get(handles.d,'String')); %získání zadaného koeficientu d
d=a-(a-(d*0.01*a));%prevod z poklesu vykonu/rok na pokles ucinnosti/rok %převod z %/rok na desetinné číslo

datum=get(handles.datum,'String'); %získání data uvedení do provozu
if length(datum)~=10
    errordlg('Nesprávný formát data. Datum musí být ve formátu dd/mm/rrrr',...
			'Incorrect Input','modal')
end
%dnešní datum ve formátu mm/dd/yyyy 
formatOut = 'mm/dd/yyyy ';
dnes=datestr(now,formatOut);
%převedení českého formátu data na anglický:
datum_en=datum;
datum_en(1:2)=datum(4:5);
datum_en(4:5)=datum(1:2);
%počet dní od uvedení do provozu do dnešního dne
e=daysact(datum_en, dnes);

%zjištění indexů sloupců vybraných teplot
TemperatureIndexes = get(handles.listboxTemperature,'Value');
%zajištění, že teplota1 bude v pořadí před teplotou 2
TemperatureIndexes = sort(TemperatureIndexes);
fileName = handles.fileName;
[hodnoty, colNames] = xlsread(fileName); %vypíše hodnoty a názvy proměnných
posun=size(colNames,2)-size(hodnoty,2);
if length(TemperatureIndexes) == 1
    %pokud je vybrán pouze jeden sloupec s teplotou, je počítáno pouze s 
    %tímto sloupcem
	c = hodnoty(:,TemperatureIndexes-posun); %index-2, protože datum a čas nejsou 
                                         %vypsány do proměnné hodnoty (nemají
                                         %číselný formát)
elseif length(TemperatureIndexes) == 2
    %pokud jsou vybrány dva sloupce s teplotou a pokud v některém sloupci
    %nejsou samé nuly- je provedeno průměrování teplot
	t1 = hodnoty(:,TemperatureIndexes(1)-posun);
	t2 = hodnoty(:,TemperatureIndexes(2)-posun);
    
    if sum(t2)==0
        c=t1;
    else
        c = (t1+t2)./2; %průměrování teplot
    end
else
    errordlg('Musí být vybrány přesně jedna nebo dvě hodnoty teploty',...
			'Incorrect Selection','modal')
end 
% Výpočet koeficientů n:
n = a * (1 + (-b * (c-25))/100 ) *(1-(d*e/365));

%zjištění indexů sloupců vybraných osvitů
ExposureIndexes = get(handles.listboxExposure,'Value');
%zajištění, že teplota1 bude v pořadí před teplotou 2
ExposureIndexess = sort(ExposureIndexes);
if length(ExposureIndexes) == 1
    %pokud je vybrán pouze jeden sloupec s osvitem, je počítáno pouze s 
    %tímto sloupcem                                     
    O = hodnoty(:,ExposureIndexes-posun); 
    
elseif length(ExposureIndexes) == 2
    %pokud jsou vybrány dva sloupce s osvitem a pokud v některém sloupci
    %nejsou samé nuly- je provedeno průměrování osvitů
	O1 = hodnoty(:,ExposureIndexes(1)-posun);
	O2 = hodnoty(:,ExposureIndexes(2)-posun);
    
    if sum(O2)==0
        O=O1;
    else
        O = (O1+O2)./2; %průměrování osvitu
    end
else
    errordlg('Musí být vybrány přesně jedna nebo dvě hodnoty osvitu',...
			'Incorrect Selection','modal')
end

VykonIndex = get(handles.listboxVykon,'Value');
vykon=hodnoty(:,VykonIndex-posun);

if posun==2 
    % získání časového kroku v minutách:
    casy= colNames(2:end,2); %časy ve formátu cell (casy v excelu jsou cteny jako text)
    casy= char(casy); %přepis do formátu string
    
    %přepis string do matice čísel: [hod min sec]
    casy_cisla=zeros(size(casy,1), 3);
    for i=1:size(casy,1)
        casy_cisla(i,1)=str2double(casy(i,1:2));
        casy_cisla(i,2)=str2double(casy(i,4:5));
        casy_cisla(i,3)=str2double(casy(i,7:8));
    end
    casy_diff = diff(casy_cisla);
    dt = 60*casy_diff(:,1)+casy_diff(:,2)+casy_diff(:,3)/60;%časový krok v minutách
    dt=[1; dt];
else %pokud není na začátku datum, čas-> nastavení časového kroku na konstatní
     %hodnotu 1 min
    dt=ones(length(n),1);
end

E_min = n.* O.* dt/(60*1000); %hodnoty minutových vzorků- v kWh

%Nastaveni casove osy k vykresleni
start = casy_cisla(1,1)+casy_cisla(1,2)/60+casy_cisla(1,2)/3600;
casova_osa = start+cumsum(dt)/60;


boxVykon= get(handles.checkboxVykon,'Value')
boxTeplota = get(handles.checkboxOsvit,'Value')
boxOsvit = get(handles.checkboxTeplota,'Value')

%pokud není vybráno nic: chybová hláška
if sum(boxVykon+boxTeplot+boxOsvit)==0
    errordlg('Musíte vybrat hodnoty k vykreslení.',...
			'Incorrect Selection','modal')

%pokud je vybrána právě jedna hodnota: vykreslení 1 grafu
elseif sum(boxVykon+boxTeplot+boxOsvit)==1
    if boxVykon==1
        plot(handles.axes1,casova_osa, vykon)
        xlabel(handles.axes1,'Čas měření [h]')
        ylabel(handles.axes1,'Výkon FVE [kW]')
    elseif boxTeplota==1
        plot(handles.axes1,casova_osa, c)
        xlabel(handles.axes1,'Čas měření [h]')
        ylabel(handles.axes1,'Teplota [°C]')
    else
        plot(handles.axes1,casova_osa, O)
        xlabel(handles.axes1,'Čas měření [h]')
        ylabel(handles.axes1,'Osvit [???]')
    end
    
elseif sum(boxVykon+boxTeplot+boxOsvit)==2
    if boxVykon==1 & boxTeplota==1
        plot(casova_osa, vykon)
        hold on
        plot(casova_osa, c, 'r')
        xlabel(handles.axes1,'Čas měření [h]')
        legend(handles.axes1, 'Výkon FVE [kW]', 'Teplota [°C]')
    elseif boxVykon==1 & boxOsvit==1
        plot(casova_osa, vykon)
        hold on
        plot(casova_osa, O, 'g')
        xlabel(handles.axes1,'Čas měření [h]')
        legend(handles.axes1, 'Výkon FVE [kW]', 'Osvit [???]')
    elseif boxTeplota==1 & boxOsvit==1
        plot(casova_osa, c, 'r')
        hold on
        plot(casova_osa, O, 'g')
        xlabel(handles.axes1,'Čas měření [h]')
        legend(handles.axes1, 'Výkon FVE [kW]', 'Teplota [°C]')
    end
end

%Vykreslení


plot(handles.axes1,casova_osa, vykon)
xlabel(handles.axes1,'Čas měření [h]')
ylabel(handles.axes1,'Výkon FVE [kW]')





%vysledna hodnota:
pocet_panelu=str2double(get(handles.panelu,'String')); %získání zadaného koeficientu d
sumEmin=sum(E_min)*1.6*pocet_panelu/1000; %suma+ kWh->MWh
set(handles.vysledna_hodnota,'String',sumEmin);

zacatek=str2double(get(handles.zacatekDne, 'String'));
konec=str2double(get(handles.konecDne, 'String'));
rozdil_hodnota=konec-zacatek;
set(handles.rozdil, 'String', rozdil_hodnota)

procento_hodnota=rozdil_hodnota/(sumEmin/100);
set(handles.procento, 'String', 100-procento_hodnota)






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


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


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


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


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


% --- Executes during object creation, after setting all properties.
function datum_CreateFcn(hObject, eventdata, handles)
% hObject    handle to datum (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 listboxTemperature.
function listboxTemperature_Callback(hObject, eventdata, handles)
% hObject    handle to listboxTemperature (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 listboxTemperature contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listboxTemperature


% --- Executes during object creation, after setting all properties.
function listboxTemperature_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listboxTemperature (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 listboxExposure.
function listboxExposure_Callback(hObject, eventdata, handles)
% hObject    handle to listboxExposure (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 listboxExposure contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listboxExposure


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


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


% --- Executes during object creation, after setting all properties.
function konecDne_CreateFcn(hObject, eventdata, handles)
% hObject    handle to konecDne (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 listboxVykon.
function listboxVykon_Callback(hObject, eventdata, handles)
% hObject    handle to listboxVykon (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 listboxVykon contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listboxVykon


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


% --- Executes on button press in checkboxOsvit.
function checkboxOsvit_Callback(hObject, eventdata, handles)
% hObject    handle to checkboxOsvit (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 checkboxOsvit


% --- Executes on button press in checkboxTeplota.
function checkboxTeplota_Callback(hObject, eventdata, handles)
% hObject    handle to checkboxTeplota (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 checkboxTeplota
