How to display a running timer in MATLAB GUI

38 visualizzazioni (ultimi 30 giorni)
Alan
Alan il 2 Lug 2012
Commentato: Nguyen il 30 Ago 2022
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
%%%%

Risposte (3)

Luffy
Luffy il 2 Lug 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 Commento
Phillip Tiam Watt
Phillip Tiam Watt il 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

Accedi per commentare.


Sean de Wolski
Sean de Wolski il 2 Lug 2012

Peer Blumido
Peer Blumido il 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 Commento
Nguyen
Nguyen il 30 Ago 2022
I have tried your method but there is an error :
"Error using uicontrol
Cannot set property to a deleted object."

Accedi per commentare.

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by