Help with timing a pushbutton being pushed twice
2 次查看(过去 30 天)
显示 更早的评论
So i'm trying to find the time interval between a single push button when pushed twice.. here is my code so far. This is for a GUI btw
% Choose default command line output for BW2
handles.output = hObject;
set(handles.pushbutton1, 'UserData', 0);
setappdata(handles.text11, 'text11', 0);
setappdata(handles.text12, 'STARTING', 0);
setappdata(handles.text13, 'FINISH', 0);
% Update handles structure
guidata(hObject, handles);
handles.gx= 0;
handles.gy= 0;
% UIWAIT makes BW2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = BW2_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)
counter = get(hObject, 'UserData') + 1;
set(hObject, 'UserData', counter);
set(handles.text1, 'String', sprintf('%d', counter));
set(handles.text8,'String',datestr(now));
if mod(counter,2)
c = clock;
seconds = c(6);
setappdata(handles.text12, 'STARTING', seconds);
guidata(hObject, handles);
else
c = clock;
seconds = c(6);
setappdata(handles.text13, 'FINISH', seconds);
guidata(hObject, handles);
end
before = getappdata(handles.text12, 'STARTING');
after = getappdata(handles.text13, 'FINISH');
interval = (after-before)
if (interval < 5)
bpm = (2/interval)*(60/1);
set(handles.text11, 'string', bpm);
end
the problem starts under the pushbutton callback. i'm basically reading in values every time they're even or odd and measuring time between each click. My first value is always something huge like 50+ seconds, and every other value is negative.
回答(2 个)
Kye Taylor
2013-2-12
编辑:Kye Taylor
2013-2-12
Computing the time elapsed between two date vectors is nontrivial, I bet you're getting some weird behavior here. Try using the etime function
t = clock
elapsedTime = etime(clock,t);
If you include the line
handles.startTime = [];
in the *_OpeningFcn callback, then the following is an example of a callback that will spit out the elapsed time between pushing the pushbutton:
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)
if isempty(handles.startTime)
handles.startTime = clock;
else
% get elapsed time
elapsedTime = etime(clock, handles.startTime);
% reset
handles.startTime = [];
% display elapsed time
disp(elapsedTime)
end
guidata(hObject, handles);
4 个评论
Sean de Wolski
2013-2-12
Yes. I would do all of this in an object but that's a different story.
Anyway, it doesn't matter if tic is started elsewhere. You can have the handle to a specific tic. Consider this:
t = tic;
pause(4);
tt=tic;
toc(t);
toc(tt);
Kye Taylor
2013-2-12
编辑:Kye Taylor
2013-2-12
Well.. once he figures out how to keep track of the state, and either pass around the outputs from tic or make them persistent, the tic/toc commands you mention are viable. I didn't realize toc could take inputs. sweet.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!