这并不是一项容易执行的任务,您可能需要考虑一下您的设计。这可以包装在一个类中,但不一定。如果您不想这样做,您可以修改底层 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