MATLAB: Timing two events in an If loop? GUI HELP. THANKS

clockguiloop

So basically i'm trying to capture the particular time (in seconds) of the first click, and then capture the time again in another click ALL of on the same pushbutton. Then i'm subtracting the time to find the interval between the two clicks. So how I went about this so far is that I made a counter for the pushbutton, and everytime it was even it would display 'even' or odd display 'odd'.. I replaced the even/odd deal with setappdata's for two different text box's where I would save a tic function in each of the either If or Else statement (tic/(1000000000)) as a matter of fact to reach it down to seconds. Obviously this didn't work and now i'm here.
So far here is my code
function clockwork_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 clockwork (see VARARGIN)
setappdata(handles.text1,'count',0)
setappdata(handles.text2,'start',0)
setappdata(handles.text3,'finish',0)
% Choose default command line output for clockwork
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes clockwork wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = clockwork_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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% limit = getappdata(handles.text1,'count');
if (handles.pushbutton1)
counter = getappdata(handles.text1,'count');
counted = counter+1;
setappdata(handles.text1,'count',counted);
count = getappdata(handles.text1,'count');
set(handles.text1,'string',count);
end
a = getappdata(handles.text1,'count');
if mod(a,2)
disp('odd')
else
disp('even')
end
So I can't really find a particular clock func to use that can give me an accurate time in seconds? I've tried tic'ing both of the If and Else and saving them in setappdata's of two separate text functions, but nothing seems to be working right..

Best Answer

I am not really sure what you are asking, nor can I figure out why the code you posted is relevant... My guess is you are doing something conceptually wrong since the statement (tic/(1000000000)) is nonsensical. You should assume that tic returns a meaningless number that has nothing to do with timing. What you want is toc, which returns the elapsed time in seconds since tic was called.
On odd number of presses you can call tic and on even number of button presses you can call toc. This isn't particularly robust (if something else calls tic in between button presses everything will get messed up). Better might be to call x = tic and save x on odd button presses and then call toc(x) on odd button presses. You can also use clock and etime.
Related Question