How to display a running timer in MATLAB GUI
8 views (last 30 days)
Show older comments
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
%%%%
0 Comments
Answers (3)
Luffy
on 2 Jul 2012
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 Comment
Phillip Tiam Watt
on 28 Feb 2015
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
Peer Blumido
on 18 Feb 2020
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 Comment
Nguyen
on 30 Aug 2022
I have tried your method but there is an error :
"Error using uicontrol
Cannot set property to a deleted object."
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!