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

% Last Modified by GUIDE v2.5 22-Jun-2011 09:31:02

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = Wirtschaftlichkeit_3_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 Paechter_Anzahl_input_Callback(hObject, eventdata, handles)
% hObject    handle to Paechter_Anzahl_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 Paechter_Anzahl_input as text
%        str2double(get(hObject,'String')) returns contents of Paechter_Anzahl_input as a double


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



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


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



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


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

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


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


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


% --- Executes during object creation, after setting all properties.
function popupmenuWEA_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenuWEA (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 LoadArcview.
function LoadArcview_Callback(hObject, eventdata, handles)
% hObject    handle to LoadArcview (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');
guidata(hObject,handles)

setPopupmenuString(handles.popupmenuWEA,eventdata,handles);

function setPopupmenuString(hObject,eventdata,handles)

fileName = handles.fileName;
Tabelle=xlsread(fileName);
WEA_Anzahl=unique(Tabelle(:,1));
set(handles.popupmenuWEA,'string',WEA_Anzahl); 


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

names = get(handles.popupmenuWEA,'String');
idx = get(handles.popupmenuWEA,'Value');
selected_name = names(idx); 
Auswahl=str2double(selected_name);


fileName = handles.fileName;
[num,txt,raw]=xlsread(fileName);

zelle=num(:,1);
zeile = find(zelle(:,1) == Auswahl);

for variable=1:size(zeile)
    wert=zeile(variable,:)+1;
    uebergabe(variable,:)={raw{wert,:}};
    set(handles.uitable1,'data',uebergabe);
end;

%Mindestverguetung1_rund_output
%Mindestverguetung2_rund_output

Ausschuettung1_12=str2double(get(handles.Verguetung1_12,'String'));
Ausschuettung13_20=str2double(get(handles.Verguetung13_20,'String'));
Paechter=str2double(get(handles.Paechter_Anzahl_input,'String'));
Bewirtschafter=str2double(get(handles.Bewirtschafter_Anzahl_input,'String'));
Grunddienstbarkeit=get(handles.Grunddienstbarkeit_Anzahl_input,'String');
Tabelle_neu=get(handles.uitable1,'Data');
Zuordnung=Tabelle_neu(:,7);
Position=Tabelle_neu;

z=0;
for Flst=1:size(Zuordnung)
    Zeile1=Zuordnung(Flst,1);
    if strcmp(Zeile1, 'GDB')
        z=z+1;
        Position{Flst,9}=Grunddienstbarkeit;
        Position{Flst,10}=Grunddienstbarkeit;
    elseif strcmp(Zeile1, 'Baulast')
        z=z+1;
        Position{Flst,9}=Grunddienstbarkeit;
        Position{Flst,10}=Grunddienstbarkeit;
    elseif strcmp(Zeile1, 'Abstandsflaeche')
        z=z+1;
        Position{Flst,9}=Grunddienstbarkeit;
        Position{Flst,10}=Grunddienstbarkeit;
    elseif strcmp(Zeile1, 'Abstandsfläche')
        z=z+1;
        Position{Flst,9}=Grunddienstbarkeit;
        Position{Flst,10}=Grunddienstbarkeit;
    else
    end;  
end

Summe_Grunddienstbarkeit=z*(str2double(Grunddienstbarkeit));
Ausschuettung_Mast1=Ausschuettung1_12-Paechter-Bewirtschafter-Summe_Grunddienstbarkeit;
Ausschuettung_Mast2=Ausschuettung13_20-Paechter-Bewirtschafter-Summe_Grunddienstbarkeit;

z1=0;
for Flst=1:size(Zuordnung)
    Zeile1=Zuordnung(Flst,1);
    if strcmp(Zeile1, 'Mast')
        z1=z1+1;
    elseif strcmp(Zeile1, 'mast')    
        z1=z1+1;
    elseif strcmp(Zeile1, 'MAST')    
        z1=z1+1;
    end;
end;
Mehr_Mast1=Ausschuettung_Mast1/z1;
Mehr_Mast2=Ausschuettung_Mast2/z1;
for Flst=1:size(Zuordnung)
    Zeile1=Zuordnung(Flst,1);
    if strcmp(Zeile1, 'Mast')
        Position{Flst,9}=num2str(Mehr_Mast1);
        Position{Flst,10}=num2str(Mehr_Mast2);
    elseif strcmp(Zeile1, 'mast')    
        Position{Flst,9}=num2str(Mehr_Mast1);
        Position{Flst,10}=num2str(Mehr_Mast2);
    elseif strcmp(Zeile1, 'MAST')    
        Position{Flst,9}=num2str(Mehr_Mast1);
        Position{Flst,10}=num2str(Mehr_Mast2);
    end;
    
    Position{Flst,11}=false;
    Position{Flst,12}=0;
    Position{Flst,13}=0;
end;


set(handles.uitable1,'Data',Position);

guidata(hObject,handles)


% --- Executes when entered data in editable cell(s) in uitable1.
function uitable1_CellEditCallback(hObject, eventdata, handles)
% hObject    handle to uitable1 (see GCBO)
% eventdata  structure with the following fields (see UITABLE)
%	Indices: row and column indices of the cell(s) edited
%	PreviousData: previous data for the cell(s) edited
%	EditData: string(s) entered by the user
%	NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
%	Error: error string when failed to convert EditData to appropriate value for Data
% handles    structure with handles and user data (see GUIDATA)

Paechter=str2double(get(handles.Paechter_Anzahl_input,'String'));
Bewirtschafter=str2double(get(handles.Bewirtschafter_Anzahl_input,'String'));

Tabelle_neu1=get(hObject,'Data');

Zuordnung=Tabelle_neu1(:,11);

for Flst=1:length(Zuordnung)    

    if Tabelle_neu1{Flst,11}==1 
        Wert(Flst,1)=str2double(Tabelle_neu1(Flst,12));
        Wert(Flst,2)=str2double(Tabelle_neu1(Flst,13));
        
    else
        Wert(Flst,1)=str2double(Tabelle_neu1(Flst,9));
        Wert(Flst,2)=str2double(Tabelle_neu1(Flst,10));
        
    end;
end;
Summe_1_12=sum(Wert(:,1))+Paechter+Bewirtschafter;
Summe_13_20=sum(Wert(:,2))+Paechter+Bewirtschafter;

set(handles.Summe_1_12,'String',Summe_1_12);
set(handles.Summe_13_20,'String',Summe_13_20);

guidata(hObject,handles)
    
