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

% Last Modified by GUIDE v2.5 20-Apr-2018 07:43:27
clc
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Fuzzyregelung_OpeningFcn, ...
                   'gui_OutputFcn',  @Fuzzyregelung_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 Fuzzyregelung is made visible.
function Fuzzyregelung_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 Fuzzyregelung (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

%Verbindung zum Modelworkspace
cd 'C:\Users\tobia\Desktop\Aktualisieren\Masterprojekt_Fuzzy\Praktikum\GUI_ohneLageregelung';
%load_system('GUIModell')
mdlWks = get_param('GUIModell','ModelWorkspace');
%Auslesen der Variablen im Modelworkspace
Js = num2str(getVariable(mdlWks,'Js'));
r=num2str(getVariable(mdlWks,'r'));
fw=num2str(getVariable(mdlWks,'fw'));
mp=num2str(getVariable(mdlWks,'mp'));
d=num2str(getVariable(mdlWks,'d'));
fp=num2str(getVariable(mdlWks,'fp'));
hoehe=num2str(getVariable(mdlWks,'hoehe'));
zeitsp=num2str(getVariable(mdlWks,'zeitsp'));
alphastart=num2str(getVariable(mdlWks,'alphastart'));
alphapunktstart=num2str(getVariable(mdlWks,'alphapunktstart'));
simtime=num2str(getVariable(mdlWks,'simtime'));
stepsize=num2str(getVariable(mdlWks,'stepsize'));

%Eintragen in EditTextBoxen
set(handles.editmta, 'String', Js);
set(handles.editla, 'String', r);
set(handles.editra, 'String', fw);
set(handles.editmp, 'String', mp);
set(handles.edithpl, 'String', d);
set(handles.editrp, 'String', fp);
set(handles.feld1, 'String', hoehe);
set(handles.feld2, 'String', zeitsp);
set(handles.alphastart, 'String', alphastart);
set(handles.alphapunktstart, 'String', alphapunktstart);
set(handles.simulationszeit, 'String', simtime);
set(handles.schrittweite, 'String', stepsize);

% Pendelbild  einfügen 
axes(handles.pendelbild);            % Auswahl des entsprechenden Axes-Objekts 
G1=imread('pendelbild.jpg','jpg');   % Einlesen der Grafik 
image (G1); axis image;              % Grafik ausgeben, Grafik entzerren 
axis off                             % Koordinatenachsen ausblenden    

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


% --- Outputs from this function are returned to the command line.
function varargout = Fuzzyregelung_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 simulieren.
function simulieren_Callback(hObject, eventdata, handles)
% hObject    handle to simulieren (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%Auswahl des Verzeichnises, Laden der FIS und Übergeben an den
%Modellworkspace
%cd 'C:\Users\tobia\Desktop\Aktualisieren\Masterprojekt_Fuzzy\Praktikum\GUI';
fuzzyregler=readfis('invrotpen_Fuzzyregler.fis');
assignin('base', 'fuzzyregler', fuzzyregler);

%Übergeben der Parameter aus den Edit Text Boxen an den Modellworkspace
load_system('GUIModell')
mdlWks = get_param('GUIModell','ModelWorkspace');

%Js Massenträgheit des Arms berzüglich des Drehpunkts
Js=str2num(get(handles.editmta, 'String'));
assignin(mdlWks,'Js',Js)

%r Länge des Arms
r=str2num(get(handles.editla, 'String'));
assignin(mdlWks,'r',r)

%fw Reibungskoeffizient im Drehpunkt des Armms
fw=str2num(get(handles.editra, 'String'));
assignin(mdlWks,'fw',fw)

%m Masse des Pendels
m=str2num(get(handles.editmp, 'String'));
assignin(mdlWks,'m',m)

%d Halbe Pendellänge
d=str2num(get(handles.edithpl, 'String'));
assignin(mdlWks,'d',d)

%fp Reibungskeoffizient im Drehpunkt des Pendels
fp=str2num(get(handles.editrp, 'String'));
assignin(mdlWks,'fp',fp)

%##########################################################################
%Festlegen der Art der Führungsgröße
fuehrungsgroesse=get(handles.fuhrung, 'value');
assignin(mdlWks,'fuehrungsgroesse',fuehrungsgroesse)

%Übergeben der Parameter der Führungsgröße
if fuehrungsgroesse == 1
    hoehe=str2num(get(handles.feld1, 'String'));
    assignin(mdlWks,'hoehe',hoehe)
    zeitsp=str2num(get(handles.feld2, 'String'));
    assignin(mdlWks,'zeitsp',zeitsp)
end
if fuehrungsgroesse == 2
    steigung=str2num(get(handles.feld1, 'String'));
    assignin(mdlWks,'steigung',steigung)
    zeitr=str2num(get(handles.feld2, 'String'));
    assignin(mdlWks,'zeitr',zeitr)
end
if fuehrungsgroesse == 3
    frequenz=str2num(get(handles.feld1, 'String'));
    assignin(mdlWks,'frequenz',frequenz)
    amplitude=str2num(get(handles.feld2, 'String'));
    assignin(mdlWks,'amplitude',amplitude)
end

%##########################################################################
%Übergeben der Anfangsbedingungen
alphastart=str2num(get(handles.alphastart, 'String'));
assignin(mdlWks,'alphastart',alphastart)
alphapunktstart=str2num(get(handles.alphapunktstart, 'String'));
assignin(mdlWks,'alphapunktstart',alphapunktstart)

%##########################################################################
%Festlegen der Simulationsparameter
simtime=str2num(get(handles.simulationszeit, 'String'));
assignin(mdlWks,'simtime',simtime)
stepsize=str2num(get(handles.schrittweite, 'String'));
assignin(mdlWks,'stepsize',stepsize)

%##########################################################################
%Starten der Simulation
simulate=sim('GUIModell');

%##########################################################################
%Plotten der Simulationsergebnisse
assignin('base', 'time', time);

%Alphapunkt
axes(handles.axes4);
plot(time, alphadot)
assignin('base', 'alphadot', alphadot);

%Alpha
axes(handles.axes1);
plot(time, alfa)
assignin('base', 'alfa', alfa);

%Moment
axes(handles.axes6);
plot(time, M)
assignin('base', 'M', M);

%##########################################################################


% --- Executes on button press in fiseditorb.
function fiseditorb_Callback(hObject, eventdata, handles)
% hObject    handle to fiseditorb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
fuzzyregler=readfis('invrotpen_Fuzzyregler.fis');
assignin('base', 'fuzzyregler', fuzzyregler);
fuzzy(fuzzyregler)



% --- Executes on button press in mfeditorb.
function mfeditorb_Callback(hObject, eventdata, handles)
% hObject    handle to mfeditorb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
fuzzyregler=readfis('invrotpen_Fuzzyregler.fis');
assignin('base', 'fuzzyregler', fuzzyregler);
mfedit(fuzzyregler)


% --- Executes on button press in ruleeditorb.
function ruleeditorb_Callback(hObject, eventdata, handles)
% hObject    handle to ruleeditorb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
fuzzyregler=readfis('invrotpen_Fuzzyregler.fis');
assignin('base', 'fuzzyregler', fuzzyregler);
ruleedit(fuzzyregler)


% --- Executes on button press in vollba.
function vollba_Callback(hObject, eventdata, handles)
% hObject    handle to vollba (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
alfa = evalin('base', 'alfa');
time = evalin('base', 'time');
figure;
plot(time, alfa)
grid on


% --- Executes on button press in vollbad.
function vollbad_Callback(hObject, eventdata, handles)
% hObject    handle to vollbad (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
alphadot = evalin('base', 'alphadot');
time = evalin('base', 'time');
figure;
plot(time, alphadot)
grid on


% --- Executes on button press in vollbm.
function vollbm_Callback(hObject, eventdata, handles)
% hObject    handle to vollbm (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
m = evalin('base', 'M');
time = evalin('base', 'time');
figure;
plot(m)
grid on


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


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



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



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



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


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


% --- Executes during object creation, after setting all properties.
function editrp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editrp (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 fuhrung.
function fuhrung_Callback(hObject, eventdata, handles)
% hObject    handle to fuhrung (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 fuhrung contents as cell array
%        contents{get(hObject,'Value')} returns selected item from fuhrung
%fuhrung=get(handles.fuhrung, 'value');

switch get(handles.fuhrung, 'value')
case 1
  set(handles.sprungtext, 'String', 'Sprunghöhe');
  set(handles.zeittext, 'String', 'Zeitpunkt');
  set(handles.feld3, 'String', '°');
  set(handles.feld4, 'String', 's');
case 2
  set(handles.sprungtext, 'String', 'Steigung');
  set(handles.zeittext, 'String', 'Zeitpunkt');
  set(handles.feld3, 'String', '°/s');
  set(handles.feld4, 'String', 's');
case 3
  set(handles.sprungtext, 'String', 'Frequenz');
  set(handles.zeittext, 'String', 'Amplitude');
  set(handles.feld3, 'String', 'Hz');
  set(handles.feld4, 'String', '°');
end


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


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


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


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


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


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


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