How to display a running timer in MATLAB GUI

17 次查看(过去 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 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by