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

% Last Modified by GUIDE v2.5 23-Nov-2012 19:51:00

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

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

% Update handles structure
guidata(hObject, handles);

new_plot_R(handles);
global Schaltung;% Variable welche Schaltung ausgewählt ist
Schaltung='RButton'; % Bestimmt das R Button zuerst ausgewählt
global Bereich;
Bereich='Zeitbereich';
% UIWAIT makes Momentanleistung wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Momentanleistung_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 mouse press over figure background.
function figure1_ButtonDownFcn(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)


% --- Executes on selection change in FrequenzZeit.
% Dropdown menü für Zeit oder Frequenz
function FrequenzZeit_Callback(hObject, eventdata, handles)
% hObject    handle to FrequenzZeit (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 FrequenzZeit contents as cell array
%        contents{get(hObject,'Value')} returns selected item from FrequenzZeit
global Bereich;
global Schaltung;
contents = cellstr(get(hObject,'String'));
Bereich=contents{get(hObject,'Value')};
if strcmp(Bereich,'Zeitbereich')
    set(handles.UI_plot,'Visible','on');
    set(handles.P_plot,'Visible','on');
    set(handles.Real_plot,'Visible','off');
    set(handles.Imag_plot,'Visible','off');
    set(handles.Betrag_plot,'Visible','off');  
end
if strcmp(Bereich,'Frequenzbereich')
    set(handles.UI_plot,'Visible','off');
    set(handles.P_plot,'Visible','off');
    set(handles.Real_plot,'Visible','on');
    set(handles.Imag_plot,'Visible','on');
    set(handles.Betrag_plot,'Visible','on');
end
switch (Schaltung)
    case 'RButton'
        new_plot_R(handles)
    case 'RCButton'
        new_plot_C(handles)
    case 'RLButton'
        new_plot_L(handles)
    case 'RLCButton'
        new_plot_C(handles)
end




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



function new_plot_R(handles) %params beinhaltet alle Parameter die durch die GUI verstellbar sind
%wird aufgerufen sobald etwas verstellt wird, liest einmal alle Werte aus
%und startet die Berechnung und zeichnen neu

params.R=str2double(get(handles.RWert,'String'));
params.f=str2double(get(handles.FWert,'String'));

data=Widerstand(params.R,params.f);

global Bereich;
if strcmp(Bereich,'Zeitbereich')
    Leistungsdiagramme(data,handles);
else Frequenzdiagramme(data,handles);
end

%% handles sind Zeiger auf Objekte
function new_plot_C(handles) %params beinhaltet alle Parameter die durch die GUI verstellbar sind
%wird aufgerufen sobald etwas verstellt wird, liest einmal alle Werte aus
%und startet die Berechnung und zeichnen neu

params.R=str2double(get(handles.RWert,'String'));
params.C=str2double(get(handles.CWert,'String'));
params.C=params.C*1e-7;

params.f=str2double(get(handles.FWert,'String'));
data=WiderstandKapazitaet(params.R,params.f,params.C);

global Bereich;
if strcmp(Bereich,'Zeitbereich')
    Leistungsdiagramme(data,handles);
else Frequenzdiagramme(data,handles);
end

function new_plot_L(handles) %params beinhaltet alle Parameter die durch die GUI verstellbar sind
%wird aufgerufen sobald etwas verstellt wird, liest einmal alle Werte aus
%und startet die Berechnung und zeichnen neu

params.R=str2double(get(handles.RWert,'String')); % Was lese ich aus Inspector
params.L=str2double(get(handles.LWert,'String'));
params.L=params.L*1e-3;


params.f=str2double(get(handles.FWert,'String'));
data=WiderstandInduktivitaet(params.R,params.f,params.L);

global Bereich;
if strcmp(Bereich,'Zeitbereich')
    Leistungsdiagramme(data,handles);
else Frequenzdiagramme(data,handles);
end

function new_plot_RLC(handles) %params beinhaltet alle Parameter die durch die GUI verstellbar sind
%wird aufgerufen sobald etwas verstellt wird, liest einmal alle Werte aus
%und startet die Berechnung und zeichnen neu

params.R=str2double(get(handles.RWert,'String'));
params.C=str2double(get(handles.CWert,'String'));
params.C=params.C*1e-7;
params.L=str2double(get(handles.LWert,'String'));
params.L=params.L*1e-3;

params.f=str2double(get(handles.FWert,'String'));

data=WiderstandKapazitaetInduktivitaet(params.R,params.f,params.C,params.L);

global Bereich;
if strcmp(Bereich,'Zeitbereich')
    Leistungsdiagramme(data,handles);
else Frequenzdiagramme(data,handles);
end



function RWert_Callback(hObject, eventdata, handles)
% hObject    handle to RWert (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 RWert as text
%        str2double(get(hObject,'String')) returns contents of RWert as a double
R_Value=get(hObject,'String');
if isnumeric(R_Value)
    R_ValueNum=str2double(R_Value);
    if R_ValueNum<=1000
        if R_ValueNum>=1
            set(handles.RSlider,'Value',R_ValueNum)
        end
    end
end

global Schaltung;
switch (Schaltung)
    case 'RButton'
        new_plot_R(handles)
    case 'RCButton'
        new_plot_C(handles)
    case 'RLButton'
        new_plot_L(handles)
    case 'RLCButton'
        new_plot_RLC(handles)
end

% --- Executes during object creation, after setting all properties.
function RWert_CreateFcn(hObject, eventdata, handles)
% hObject    handle to RWert (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');
set(hObject.String,'String',param);
end



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

global Schaltung;
switch (Schaltung)
    case 'RCButton'
        new_plot_C(handles)
    case 'RLCButton'
        new_plot_RLC(handles)
end


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

global Schaltung;
switch (Schaltung)
    case 'RLButton'
        new_plot_L(handles)
    case 'RLCButton'
        new_plot_RLC(handles)
end

% --- Executes during object creation, after setting all properties.
function LWert_CreateFcn(hObject, eventdata, handles)
% hObject    handle to LWert (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');
set(hObject.String,'String',param);
end


% --- Executes on slider movement.
function RSlider_Callback(hObject, eventdata, handles)
% hObject    handle to RSlider (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
R_Value=get(hObject,'Value');
R_Value_2=round(R_Value);
set(handles.RWert,'String',R_Value_2);

global Schaltung;
switch (Schaltung)
    case 'RButton'
        new_plot_R(handles)
    case 'RCButton'
        new_plot_C(handles)
    case 'RLButton'
        new_plot_L(handles)
    case 'RLCButton'
        new_plot_RLC(handles)
end


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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
set(hObject,'Min',1);
set(hObject,'Max',1000);



% --- Executes on slider movement.
function CSlider_Callback(hObject, eventdata, handles)
% hObject    handle to CSlider (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

C_Value=get(hObject,'Value');
C_Value_2=round(C_Value); %Wert des Sliders runden um nur ganze Schritte zuzulassen
set(hObject,'Value',C_Value_2); %Slider auf gerundeten Wert setzen
C_Value_2=10^(-7+C_Value_2); %richtigen Wert ausrechnen
set(handles.CWert,'String',C_Value_2*1e6); %Textfeld auf Wert in µF setzen

global Schaltung;
switch (Schaltung)
    case 'RCButton'
        new_plot_C(handles)
    case 'RLCButton'
        new_plot_RLC(handles)
end



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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

set(hObject,'Min',1);
set(hObject,'Max',6);
set(hObject,'Value',1);


% --- Executes on slider movement.
function LSlider_Callback(hObject, eventdata, handles)
% hObject    handle to LSlider (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

L_Value=get(hObject,'Value');
L_Value_2=round(L_Value);
L_Value_2=10^(-4+L_Value_2);
set(handles.LWert,'String',L_Value_2);

global Schaltung;
switch (Schaltung)
    case 'RLButton'
        new_plot_L(handles)
    case 'RLCButton'
        new_plot_RLC(handles)
end

% --- Executes during object creation, after setting all properties.
function LSlider_CreateFcn(hObject, eventdata, handles) %#ok<*INUSD,*DEFNU>
% hObject    handle to LSlider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
set(hObject,'Min',1);
set(hObject,'Max',10);
set(hObject,'Value',3);



% --- Executes on slider movement.
function FSlider_Callback(hObject, eventdata, handles)
% hObject    handle to FSlider (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
F_Value=get(hObject,'Value');
F_Value_2=round(F_Value);
set(hObject,'Value',F_Value_2);
set(handles.FWert,'String',F_Value_2);

global Schaltung
switch (Schaltung)
    case 'RButton'
        new_plot_R(handles)
    case 'RCButton'
        new_plot_C(handles)
    case 'RLButton'
        new_plot_L(handles)
    case 'RLCButton'
        new_plot_RLC(handles)
end
% --- Executes during object creation, after setting all properties.
function FSlider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to FSlider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
set(hObject,'Min',0);
set(hObject,'Max',1000);



function FWert_Callback(hObject, eventdata, handles)
% hObject    handle to FWert (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 FWert as text
%        str2double(get(hObject,'String')) returns contents of FWert as a double
global Schaltung
switch (Schaltung)
    case 'RButton'
        new_plot_R(handles)
    case 'RCButton'
        new_plot_C(handles)
    case 'RLButton'
        new_plot_L(handles)
    case 'RLCButton'
        new_plot_RLC(handles)
end


% --- Executes during object creation, after setting all properties.
function FWert_CreateFcn(hObject, eventdata, handles)
% hObject    handle to FWert (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 when selected object is changed in AuswahlSchaltung.
function AuswahlSchaltung_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in AuswahlSchaltung 
% 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)
global Schaltung;
Schaltung=get(eventdata.NewValue,'Tag');

switch (Schaltung)
    case 'RButton'
        new_plot_R(handles)
    case 'RCButton'
        new_plot_C(handles)
    case 'RLButton'
        new_plot_L(handles)
    case 'RLCButton'
        new_plot_RLC(handles)
end


% --- Executes when uipanel2 is resized.
function uipanel2_ResizeFcn(hObject, eventdata, handles)
% hObject    handle to uipanel2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
