How to update date and time in matlab gui -


function demo1()         h.f = figure('name','demo1');         set(h.f,'units','pixels','position',get(0,'screensize'));% adjust figure size per screen size         h.pb1 = uicontrol('style','push',...                          'units','pixels',...                          'position',[400 800 280 30],...                          'fontsize',14,...                          'string', datestr(now)); % datestr(now) used current date , time                  end  

how can real time clock in gui

this not easy task perform , may want think design. wrapped in class, not necessarily. in case not want may able modify underlying java objects, seems overworking this. have instead structured in more c-style manner placing data in struct , writing functions taking struct argument. however, since matlab not support passing memory locations need return struct after modifying it.

i have chosen use timer object this. need store timer object somewhere since needs deleted when not using anymore. further, have added code using start function , stop function well. timer object. seeing object gets finished during development phase. not needed final project. further, may want handle case window closed. cause current implementation crash since timer independent of figure , not stop when figure closed. want call stop_timer on closing figure. anyway here code:

function test() h.f = figure('name','demo1'); set(h.f,'units','pixels','position',get(0,'screensize'));% adjust figure size per screen size h.pb1 = uicontrol('style','push',...         'units','pixels',...         'position',[400 800 280 30],...         'fontsize',14,...         'string', datestr(now)); % datestr(now) used current date , time h = start_clock(h); pause on; pause(15); pause off; h = stop_clock(h);     end  function obj = start_clock(obj) %taskstoexecute calls timer object n times t = timer('startdelay', 0, 'period', 1,... % 'taskstoexecute', inf, ...       'executionmode', 'fixedrate'); t.startfcn = {@my_callback_fcn, 'my start message'}; t.stopfcn = { @my_callback_fcn, 'my stop message'}; t.timerfcn = {@set_time, obj}; obj.t = t; start(obj.t); end  function obj = stop_clock(obj) stop(obj.t); delete(obj.t); end  function set_time(obj, event, arg)     arg.pb1.string = datestr(now); end  function my_callback_fcn(obj, event, arg) txt1 = ' event occurred @ '; txt2 = arg; event_type = event.type; event_time = datestr(event.data.time); msg = [event_type txt1 event_time]; disp(msg) disp(txt2) end 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -