How to display a running timer in MATLAB GUI

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
%%%%

Réponses (3)

Luffy
Luffy le 2 Juil 2012

0 votes

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 commentaire

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

Connectez-vous pour commenter.

Peer Blumido
Peer Blumido le 18 Fév 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 commentaire

Nguyen
Nguyen le 30 Août 2022
I have tried your method but there is an error :
"Error using uicontrol
Cannot set property to a deleted object."

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interactive Control and Callbacks dans Centre d'aide et File Exchange

Question posée :

le 2 Juil 2012

Commenté :

le 30 Août 2022

Community Treasure Hunt

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

Start Hunting!

Translated by