Date: Jan 6, 2013 12:17 AM
Author: Nima Nikvand
Subject: Matlab GUI with timer in the background

Hello everyone,

I have a GUI (developed using guide) and I need to show the current time and date in one of its corners, I have written a simple code which works alright, however I am not sure where in the GUI code to use it. When I use the code in GUI_OpeningFcn, which executes just before gui is made visible, it shows the time but MATLAB remains busy and I won't be able to run anything else from the command window, furthermore, I am not sure how to stop the timer and delete the timer when the GUI is closed. When I use handles to save timer created in OpeningFcn and later delete the timer in OutputFcn, I get an error saying my handle is invalid! The code is posted below and I appreciate any help with this.

timex=timer;

set(timex,'executionMode','fixedRate');
set(timex,'timerFcn',{@timexup,handles});
set(timex,'period',1);
start(timex);
%handles.output=0;
if(~ishandle(handles))
stop(timex);
delete(timex);
end

function timexup(obj,event,handles)
set(handles.text6,'string','');
pause(0.5);
set(handles.text6,'string',num2str(datestr(now)));
set(handles.text6,'Fontsize', 10, 'FontWeight', 'bold');
pause(0.5);