Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

display a clock in a gui matlab in textbox?

1,267 views
Skip to first unread message

Francki

unread,
Jan 20, 2010, 8:40:37 PM1/20/10
to
basically, I've an application gui in which I'd like to display the time in a text box or static text box that I created... I mean I'm not talking about pressing a button and seing the digital clock or a code that automatically creates a figure to show this digital clock...no, I'm talking about displaying the time within the application i created.....can any1 help please...this is the code I have so far:
-----------------------------------------------------------------------------------------------
% --- Executes during object creation, after setting all properties.
function text3_CreateFcn(hObject, eventdata, handles)---->name of textbox:text3
t = timer;
set(t,'ExecutionMode','fixedRate',...
'Period',1,'TimerFcn',@UpdateClock);
setappdata(0,'ClockHandle',text3)
start(t)

function UpdateClock(obj,e)
text3 = getappdata(0,'ClockHandle');
set(text3,'String',datestr(clock,13))
-----------------------------------------------------------------------------------

can anyone tell what I'm doing wrong?...an error occurs..replay asap

kind regards

Francki

unread,
Jan 22, 2010, 5:11:05 PM1/22/10
to
"Francki " <jonesfr...@yahoo.fr> wrote in message <hj8bal$mpv$1...@fred.mathworks.com>...


someone please reply

Nathan

unread,
Jan 22, 2010, 5:13:20 PM1/22/10
to

Never post and just mention that "an error occurs". Be sure to copy
and paste the error message here, as well as the code that it relates
to.

Having said that:
What did the error message tell you? Where did it error in your code?

-Nathan

Matt Fig

unread,
Jan 22, 2010, 7:09:08 PM1/22/10
to
I am updating my "35 complete GUI examples" file on the file exchange soon (changing to 41...) and this is one of the examples I cover. Here is a rough draft of that example which should help you.

P.S. Let me know if there is an error before I publish this, thanks.


function [] = GUI_clock()
% Demonstrate how to have a running clock in a GUI.
% Creates a small little GUI which displays the correct time and is updated
% every minute according to the system clock.
%
% Author: Matt Fig
% Date: 1/15/2010

S.fh = figure('units','pixels',...
'position',[300 300 180 50],...
'menubar','none',...
'name','GUI_clock',...
'numbertitle','off',...
'resize','off');
S.tx = uicontrol('style','text',...
'unit','pix',...
'position',[35 10 110 30],...
'string',datestr(now,16),...
'backgroundc',get(S.fh,'color'),...
'fontsize',18,...
'fontweight','bold',...
'foregroundcolor',[.9 .1 .1]);
STRT = 60 - str2double(datestr(now,'ss')); % So we can update every minute.
tmr = timer('Name','Reminder',...
'Period',60,... % Update the time every 60 seconds.
'StartDelay',STRT,... % In seconds.
'TasksToExecute',inf,... % number of times to update
'ExecutionMode','fixedSpacing',...
'TimerFcn',{@updater});
start(tmr); % Start the timer object.
set(S.fh,'deletefcn',{@deleter}) % Kill timer if fig is closed.

function [] = updater(varargin)
% timerfcn for the timer. If figure is deleted, so is timer.
% I use a try-catch here because timers are finicky in my
% experience.
try
set(S.tx,'string',datestr(now,16))
if ~str2double(datestr(now,'MM'))
X = load('gong');
player = audioplayer(X.y, X.Fs);
play(player) % Gong at the top of the hour.
end
clear y Fs player
catch
delete(S.fh) % Close it all down.
end
end

function [] = deleter(varargin)
% If figure is deleted, so is timer.
stop(tmr);
delete(tmr);
end
end

Francki

unread,
Jan 23, 2010, 8:43:02 AM1/23/10
to
Nathan <ngre...@gmail.com> wrote in message <1ac6e938-eda3-4606...@e27g2000yqd.googlegroups.com>...

Thanks for your reply but this is actually the problem. the code relates to a static textbox within the application gui I created..it is stand alone.....ok let me retype my original code:
--------------------------------------------------------------------------
function text3_CreateFcn(hObject, eventdata, handles)


t = timer;
set(t,'ExecutionMode','fixedRate',...
'Period',1,'TimerFcn',@UpdateClock);
setappdata(0,'ClockHandle',text3)
start(t)

function UpdateClock(obj,e)
text3 = getappdata(0,'ClockHandle');
set(text3,'String',datestr(clock,13))
----------------------------------------------------------------------

Here is the error:
------------------------------------------------------------------------------
Error in ==> ControllerBuilder>text3_CreateFcn at 1617
setappdata(0,'ClockHandle',text3)

Error in ==> gui_mainfcn at 96
feval(varargin{:});

Error in ==> ControllerBuilder at 42
gui_mainfcn(gui_State, varargin{:});

??? Error using ==> struct2handle
Error while evaluating uicontrol CreateFcn
--------------------------------------------------------------------------------------------
all I want is that when I open/load my application GUI called "controllerBuilderv1.1", This particular static textbox within my GUI displays the time in a digital format with seconds running (like a normal clock HH:MM:SS).

Your assistance would be much appreciated. thanks

if anyone out there can also hel please do so.

Francki

unread,
Jan 23, 2010, 8:52:02 AM1/23/10
to
"Matt Fig" <spam...@yahoo.com> wrote in message <hjden4$8jr$1...@fred.mathworks.com>...

Good work mate.....however, that is not quite what I was looking for...Your code creates a small gui that displays the time. However, what I want is that instead of having another gui display the time, I want a textbox within my own application gui to do the same job (i.e: display the time in HH:MM:SS that updates every seconds).
Can you help? thanks in advance.
btw, your code works well.

Kent Cabarle

unread,
Feb 6, 2011, 9:23:03 PM2/6/11
to
@Matt Fig

Good Day!

Your code in making a real time clock in a GUI works perfectly..

Is it possible for me to put your real time clock in the GUI I made?

What codes do i need to put in the GUI i made so that the clock will appear in my GUI?

Thanks a lot for the help..

kent

Aarron Erbas

unread,
May 12, 2015, 5:58:12 PM5/12/15
to
"Kent Cabarle" wrote in message <iinl27$9fg$1...@fred.mathworks.com>...
you will need to create a edit box and in the property inspecter create a createFcn it will look like this after you create the edit box in GUIDE.

function timer1_CreateFcn(hObject, eventdata, handles)
% description etc.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[1 0 0]);
end
[s] = GUI_clock(hObject)
s
guidata(hObject,handles)


-------------------------------------------------------------------------
paste the function below into a script.
-------------------------------------------------------------------------

function [s] = GUI_clock(hObject,callbackdata)
% Demonstrate how to have a running clock in a GUI.
% Creates a small little GUI which displays the correct time and is updated
% every minute according to the system clock.


s = set(hObject, 'string',datestr(now,16),...
'fontsize',18,...
'fontweight','bold',...
'foregroundcolor',[.9 .1 .1]);
STRT = 60 - str2double(datestr(now,'ss')); % So we can update every minute.
tmr = timer('Name','Reminder',...
'Period',60,... % Update the time every 60 seconds.
'StartDelay',STRT,... % In seconds.
'TasksToExecute',inf,... % number of times to update
'ExecutionMode','fixedSpacing',...
'TimerFcn',{@updater});
start(tmr); % Start the timer object.
set(hObject,'deletefcn',{@deleter}) % Kill timer if fig is closed.

function [] = updater(varargin)
% timerfcn for the timer. If figure is deleted, so is timer.
% I use a try-catch here because timers are finicky in my
% experience.
try
set(hObject,'string',datestr(now,16))
if ~str2double(datestr(now,'MM'))
X = load('gong');
player = audioplayer(X.y, X.Fs);
play(player) % Gong at the top of the hour.
end
clear y Fs player
catch
delete(s) % Close it all down.
end
end

function [] = deleter(varargin)
% If figure is deleted, so is timer.
stop(tmr);
delete(tmr);
end
end



hope that it helps you and others. thanks to the original author also.

jahangirc...@gmail.com

unread,
Jan 25, 2016, 5:57:55 PM1/25/16
to
Thanks alot for such a good work. I am working exactly on the same thing. I have used the above code but the problem is that clock is not continous. It shows time once and stops. How can it be modified to get running time in HH:MM:SS?
Thanks alot

Student new

unread,
Jan 25, 2016, 7:38:09 PM1/25/16
to
Hello,
I have the above code but it only shows the time once. Time is not being updated in HH:MM:SS like a real digital clock.
I am getting the following error:

One or more output arguments not assigned during call to "set".

Error in GUI_clock (line 2)
s = set(hObject,
'string',datestr(now,16),'fontsize',24,'fontweight','bold','foregroundcolor',[.9 .1.1]);


Thanks alot

dkm...@gmail.com

unread,
Jun 16, 2016, 10:48:44 AM6/16/16
to
Why can't i get this to work. I used the same code but displays it once not continuous. Get an error: One or more output arguments not assigned

Theepa hari

unread,
Apr 2, 2017, 5:07:07 AM4/2/17
to
dkm...@gmail.com wrote in message <6d0186c9-6a6a-49ef...@googlegroups.com>...
>> unt
Error: File: unt.m Line: 142 Column: 1
The function "GUI_clock" was closed with an 'end', but at least one other function
definition was not. To avoid confusion when using nested functions, it is illegal to use
both conventions in the same file.

i am getting above error message while executing in edit box
0 new messages