Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
display a clock in a gui matlab in textbox?

Subject: display a clock in a gui matlab in textbox?

From: Francki

Date: 21 Jan, 2010 01:40:37

Message: 1 of 7

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

Subject: display a clock in a gui matlab in textbox?

From: Francki

Date: 22 Jan, 2010 22:11:05

Message: 2 of 7

"Francki " <jonesfranckandi@yahoo.fr> wrote in message <hj8bal$mpv$1@fred.mathworks.com>...
> 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


someone please reply

Subject: display a clock in a gui matlab in textbox?

From: Nathan

Date: 22 Jan, 2010 22:13:20

Message: 3 of 7

On Jan 20, 5:40 pm, "Francki " <jonesfrancka...@yahoo.fr> wrote:
> 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

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

Subject: display a clock in a gui matlab in textbox?

From: Matt Fig

Date: 23 Jan, 2010 00:09:08

Message: 4 of 7

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

Subject: display a clock in a gui matlab in textbox?

From: Francki

Date: 23 Jan, 2010 13:43:02

Message: 5 of 7

Nathan <ngreco32@gmail.com> wrote in message <1ac6e938-eda3-4606-8cb6-f5a938d75a6e@e27g2000yqd.googlegroups.com>...
> On Jan 20, 5:40 pm, "Francki " <jonesfrancka...@yahoo.fr> wrote:
> > 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
>
> 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

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.

Subject: display a clock in a gui matlab in textbox?

From: Francki

Date: 23 Jan, 2010 13:52:02

Message: 6 of 7

"Matt Fig" <spamanon@yahoo.com> wrote in message <hjden4$8jr$1@fred.mathworks.com>...
> 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

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.

Subject: display a clock in a gui matlab in textbox?

From: Kent Cabarle

Date: 7 Feb, 2011 02:23:03

Message: 7 of 7

@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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us