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

% Last Modified by GUIDE v2.5 08-Jun-2017 22:29:48

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

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

% Update handles structure
guidata(hObject, handles);

% Open Simulation
open_system('Beispiel4_sim')

% Get the value of the gain edit box and synch the GUI
value = get_param(['Beispiel4_sim' '/Gain'],'Gain');
set(handles.edit_Gain,'String',value);

% UIWAIT makes Beispiel4_gui wait for user response (see UIRESUME)
% uiwait(handles.Sinus_gui);


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

value = get(hObject,'String');

% Update the model's gain value
set_param(['Beispiel4_sim' '/Gain'],'Gain',value)

% Update simulation if the model is running
status = get_param('Beispiel4_sim','simulationstatus');
if strcmp(status,'running')
set_param('Beispiel4_sim', 'SimulationCommand', 'Update')
end

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


% --- Executes during object creation, after setting all properties.
function edit_display_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_display (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 button press in Startstopbutton.
function Startstopbutton_Callback(hObject, eventdata, handles)
% hObject    handle to Startstopbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

mystring = get(hObject,'String');
status = get_param('Beispiel4_sim','simulationstatus');

if strcmp(mystring,'Start Simulation')
% Check the status of the simulation and start it if it's stopped
if strcmp(status,'stopped')
set_param('Beispiel4_sim','simulationcommand','Start')
end

% Update the string on the pushbutton
set(handles.Startstopbutton,'String','Stop Simulation')

cla
%cla (handles.axesplot,'reset')

elseif strcmp(mystring,'Stop Simulation')
    % Check the status of the simulation and stop it if it's running
if strcmp(status,'running')
set_param('Beispiel4_sim', 'SimulationCommand', 'Stop')
end
% Update the string on the pushbutton
set(handles.Startstopbutton,'String','Start Simulation')
% Plot results from workspace after simulation
%x = evalin('base','SineWave(:,1)');
%y = evalin('base','SineWave(:,2)');
%pHandles = plot(x,y,'Parent',handles.axesplot);
end

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

% Closes Simulink model
%close_system('Beispiel4_sim')

clear all
% clear Workspace
evalin('base','clear');
% Closes Simulink model unconditionally
bdclose('Beispiel4_sim')
% Closes GUI
close Beispiel4_gui


% --- Executes on button press in Clear.
function Clear_Callback(hObject, eventdata, handles)
% hObject    handle to Clear (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
cla (handles.axesplot,'reset')
cla (handles.axesplot1,'reset')

