How to display a running timer in MATLAB GUI

조회 수: 15 (최근 30일)
Alan
Alan 2012년 7월 2일
댓글: Nguyen 2022년 8월 30일
Hi, I'm trying create an edit text box that displays the time elapsed since pressing the push button that executes my function. I have looked online for information, but I can't seem to decipher from the info I've found exactly what I should be writing in the callback and create function for the edit text box and the callback function for the push button (that also contains the function I have written). It would be really useful for me to have a running timer in my GUI, so any help would be very much appreciated!
Thanks.
function timer_Callback(hObject, eventdata, handles)
% hObject handle to timer (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 timer as text
% str2double(get(hObject,'String')) returns contents of timer as a double
%
% --- Executes during object creation, after setting all properties.
function timer_CreateFcn(hObject, eventdata, handles)
% hObject handle to timer (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 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)
%%%%
MY FUNCTION IS CONTAINED IN HERE
%%%%

답변 (3개)

Luffy
Luffy 2012년 7월 2일
I was trying to do a similar thing except that my timer should start from times gui starts running. This code helped me, http://www.mathworks.com/matlabcentral/fileexchange/12628
  댓글 수: 1
Phillip Tiam Watt
Phillip Tiam Watt 2015년 2월 28일
how did you do your timer again? I am trying to do the same thing for my GUI but the file in the link does not work for some reason and my MATLAB freezes... Kindly help me

댓글을 달려면 로그인하십시오.


Sean de Wolski
Sean de Wolski 2012년 7월 2일

Peer Blumido
Peer Blumido 2020년 2월 18일
Hey, just an idea, it worked for me.
a = 1:10; % or maybe a higher number
%Start Button pressed
for i = 1:length(a)
a(i) = a(:,i);
uicontrol(f,'Style','text','String',a(i),...
'Position',[400,300,70,25]);
pause(1) % produces after 1sec pause a new string in your gui
%Stop Button pressed
% make uicontrol visible 'off'
end
  댓글 수: 1
Nguyen
Nguyen 2022년 8월 30일
I have tried your method but there is an error :
"Error using uicontrol
Cannot set property to a deleted object."

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by