【问题标题】:How to update date and time in matlab gui如何在matlab gui中更新日期和时间
【发布时间】:2016-04-27 05:52:19
【问题描述】:
function demo1()
        H.f = figure('Name','DEMO1');
        set(H.f,'Units','Pixels','Position',get(0,'ScreenSize'));% adjust figure size as per the screen size
        H.pb1 = uicontrol('style','push',...
                         'units','pixels',...
                         'position',[400 800 280 30],...
                         'fontsize',14,...
                         'string', datestr(now)); % datestr(now) is used to get current date and time                 
end 

如何在 gui 中获取实时时钟

【问题讨论】:

  • 我不确定这怎么可能被否决。我个人觉得这是在 matlab 中执行的一项相当复杂的任务,您需要使用计时器或使用底层 Java 对象。 +1 加星标。

标签: matlab time time-series matlab-guide


【解决方案1】:

这并不是一项容易执行的任务,您可能需要考虑一下您的设计。这可以包装在一个类中,但不一定。如果您不想这样做,您可以修改底层 Java 对象,但这似乎是过度工作。相反,我以更 C 风格的方式来构建它,将所有数据放在一个结构中,并编写以结构作为参数的函数。但是,由于 matlab 不支持传递内存位置,因此您需要在修改后返回结构。

我选择为此使用计时器对象。您需要将计时器对象存储在某处,因为当您不再使用它时需要将其删除。此外,我还添加了一些使用启动函数和停止函数的代码。对于计时器对象。这是为了看到对象在开发阶段实际上已经完成。最终项目不需要这样做。此外,您可能想要处理关闭窗口的情况。这将导致当前实现崩溃,因为计时器独立于图形并且在图形关闭时不会停止。您可能想在关闭图形时调用 stop_timer。无论如何这里是代码:

function test()
h.f = figure('Name','DEMO1');
set(h.f,'Units','Pixels','Position',get(0,'ScreenSize'));% adjust figure size as per the screen size
h.pb1 = uicontrol('style','push',...
        'units','pixels',...
        'position',[400 800 280 30],...
        'fontsize',14,...
        'string', datestr(now)); % datestr(now) is used to get current date and time
h = start_clock(h);
pause on;
pause(15);
pause off;
h = stop_clock(h);    
end

function obj = start_clock(obj)
%TasksToExecute calls the 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 at ';
txt2 = arg;
event_type = event.Type;
event_time = datestr(event.Data.time);
msg = [event_type txt1 event_time];
disp(msg)
disp(txt2)
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-17
    • 2021-09-07
    • 2021-03-16
    • 2018-09-05
    • 1970-01-01
    • 2018-09-15
    • 2023-03-04
    • 2019-07-01
    相关资源
    最近更新 更多