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

% Last Modified by GUIDE v2.5 02-Jun-2011 14:17:53

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

handles.read=[];    %timer
handles.mpn=0;   %Messpaarnummer

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

% Update handles structure
guidata(hObject, handles);

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


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


% --- Executes on button press in pb_start_sub.
function pb_start_sub_Callback(hObject, eventdata, handles)
% hObject    handle to pb_start_sub (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if ~isfield(handles,'sub_GUI') % check ob sub_GUI in handles entahlten ist.
    handles.sub_GUI = sub_GUI(handles.main_GUI,@start_timer); %die Funktion sub_GUI starten,handles übergeben und zurückgegebene handles speichern
    guidata(hObject,handles) % guidata aktualisieren
else
   figure(handles.sub_GUI); % bringt sub_GUI in den Vordergrund
end
%--------------------------------------------------------------------------



function start_timer(delta_t,vis_fcn_h, outputplace_h,handle_to_main_GUI)

main_GUI_handles=guidata(handle_to_main_GUI);
main_GUI_handles.read = timer('ExecutionMode','fixedRate','TimerFcn', {@timer_fcn, handle_to_main_GUI, vis_fcn_h, outputplace_h}, 'Period', delta_t );
guidata(handle_to_main_GUI, main_GUI_handles);
start(main_GUI_handles.read);
%--------------------------------------------------------------------------

function timer_fcn(hObject, eventdata, handle_to_main_GUI, vis_fcn_h, outputplace_h)

read_value_pair(handle_to_main_GUI);
feval(vis_fcn_h,outputplace_h,handle_to_main_GUI);
%--------------------------------------------------------------------------

function   read_value_pair (handle_to_main_GUI)

main_GUI_handles=guidata(handle_to_main_GUI);
main_GUI_handles.mpn=main_GUI_handles.mpn+1;

if main_GUI_handles.mpn== 1
    main_GUI_handles.datensatz.force(main_GUI_handles.mpn)=0.04;
    main_GUI_handles.datensatz.length(main_GUI_handles.mpn)=0.05;
else
    main_GUI_handles.datensatz.force(main_GUI_handles.mpn)= main_GUI_handles.datensatz.force(main_GUI_handles.mpn-1) + randi([1,10])/10000;
    main_GUI_handles.datensatz.length(main_GUI_handles.mpn)= main_GUI_handles.datensatz.length(main_GUI_handles.mpn-1)+ randi([1,10])/10000;
end

guidata(handle_to_main_GUI, main_GUI_handles);
%--------------------------------------------------------------------------

function  plot_force(outputplace_h, handle_to_main_GUI)

main_GUI_handles=guidata(handle_to_main_GUI);
plot(outputplace_h,[main_GUI_handles.datensatz.force(main_GUI_handles.mpn),main_GUI_handles.datensatz.force(main_GUI_handles.mpn)],[0, 10]);
drawnow;
