Matlab 中使用定时器的回调函数

发布于 2024-11-28 01:34:43 字数 778 浏览 0 评论 0原文

我正在 MATLAB 中研究内容分发服务器的统计模型,并决定使用 OO 编程。这是我第一次使用 MATLAB 涉足面向对象,但我遇到了障碍。我正在尝试对服务器的下载连接进行建模,目前它只是一个 MATLAB 计时器和一个布尔值。当计时器到期时,我想将 isActive 字段从 true 设置为 false。我觉得很简单,但后来我已经为此奋斗了一天多了。以下是迄今为止该类的代码:

    classdef dl<handle
        properties
            isActive = true
            ttl = 0
        end
        methods
            function this = startTimer(this, varargin)
                this.ttl = timer('TimerFcn', @()killConnection(this), 'StartDelay',1);     
                start(this.ttl);            
            end
        end

        methods (Access = private)
            function obj = killConnection(obj, varargin)
                obj.isActive = false;
            end        
        end
    end

I am working on a statistical model of a content distribution server in MATLAB and have decided to use OO programming. This is my first foray into OO with MATLAB and I have hit a snag. I am attempting to model a download connection to the server, at the moment it is just a MATLAB timer and a boolean. When the timer expires I want to set the isActive field from true to false. So quite simple I feel but then I have been battling with this for more then a day now. Below is the code for the class so far:

    classdef dl<handle
        properties
            isActive = true
            ttl = 0
        end
        methods
            function this = startTimer(this, varargin)
                this.ttl = timer('TimerFcn', @()killConnection(this), 'StartDelay',1);     
                start(this.ttl);            
            end
        end

        methods (Access = private)
            function obj = killConnection(obj, varargin)
                obj.isActive = false;
            end        
        end
    end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

地狱即天堂 2024-12-05 01:34:43

我解决了我遇到的问题,问题出在回调处理程序的声明方式上。我不确定确切的原因,但如果有人感兴趣,这里有更好的解释,请参阅这篇博客文章 http://syncor.blogspot.com/2011/01/matlabusing-callbacks-in-classdef.html

以下是我为获得成功操作所做的更改。首先,我将回调函数更改为回调的正确结构:

    function killConnection(event, string_arg, this)

然后我在计时器中以不同的方式声明回调:

    this.ttl = timer('TimerFcn', {@dl.killConnection, this}, 'StartDelay',1);

这对我有用。感谢您的帮助,这对我来说真的很重要:P。

I solved the problem I was having, the issue was in the way the callback handler was declared. Im not sure if the precise reason but there is a better explanation here if anyone is interested, see this blog post http://syncor.blogspot.com/2011/01/matlabusing-callbacks-in-classdef.html.

Here are the changes I made to get successful operation. Firstly i changed the callback function into the proper structure for the callback:

    function killConnection(event, string_arg, this)

Then I declared the callback differently in the timer:

    this.ttl = timer('TimerFcn', {@dl.killConnection, this}, 'StartDelay',1);

This worked for me. Thanks for the help it was really getting to me :P.

人事已非 2024-12-05 01:34:43

我未经尝试的猜测是,回调需要是静态类函数,并且参数列表需要包含计时器的正确参数。然后,静态类回调需要找到对象引用来设置实例 isActive 标志。 findobj 可能会通过名称获取类对象实例,因为您选择使用句柄对象,但这可能会影响实时响应。

this.ttl = timer('TimerFcn', @dl.killConnection, 'StartDelay',1); 


methods(Static)
      function killConnection(obj, event, string_arg)
        ...
      end
end

只是一个猜测。祝你好运,我对真正的答案感兴趣,因为我最近一直在考虑尝试这个。

My guess without trying it, is that the callback needs to be a static class function and the argument list needs to be with the proper parameters for a timer. The static class callback would then need to locate the object reference to set the instance isActive flag. findobj might get the class object instance by name since you chose to use a handle object but that could affect the real-time response.

this.ttl = timer('TimerFcn', @dl.killConnection, 'StartDelay',1); 


methods(Static)
      function killConnection(obj, event, string_arg)
        ...
      end
end

Just a guess. Good luck, I'm interested in the real answer since I had been thinking about trying this just recently.

温柔一刀 2024-12-05 01:34:43

---- TimerHandle.m ---------

classdef TimerHandle < handle    
    properties
        replay_timer
        count = 0
    end
    methods
        function register_timer(obj)
            obj.replay_timer = timer('TimerFcn', {@obj.on_timer}, 'ExecutionMode', 'fixedSpacing', ...
                'Period', 1, 'BusyMode', 'drop', 'TasksToExecute', inf);
        end
        function on_timer(obj, varargin)
            obj.count = obj.count + 1;
            fprintf('[%d] on_timer()\n', obj.count);
        end
        function delete(obj)
            delete(obj.replay_timer);
            obj.delete@handle();
        end
    end
end

用法:

>> th = TimerHandle;
>> th.register_timer
>> start(th.replay_timer)
[1] on_timer()
[2] on_timer()
[3] on_timer()
[4] on_timer()
[5] on_timer()
[6] on_timer()
[7] on_timer()
[8] on_timer()
[9] on_timer()
>> stop(th.replay_timer)
>> delete(th)

---- TimerHandle.m ---------

classdef TimerHandle < handle    
    properties
        replay_timer
        count = 0
    end
    methods
        function register_timer(obj)
            obj.replay_timer = timer('TimerFcn', {@obj.on_timer}, 'ExecutionMode', 'fixedSpacing', ...
                'Period', 1, 'BusyMode', 'drop', 'TasksToExecute', inf);
        end
        function on_timer(obj, varargin)
            obj.count = obj.count + 1;
            fprintf('[%d] on_timer()\n', obj.count);
        end
        function delete(obj)
            delete(obj.replay_timer);
            obj.delete@handle();
        end
    end
end

Usage:

>> th = TimerHandle;
>> th.register_timer
>> start(th.replay_timer)
[1] on_timer()
[2] on_timer()
[3] on_timer()
[4] on_timer()
[5] on_timer()
[6] on_timer()
[7] on_timer()
[8] on_timer()
[9] on_timer()
>> stop(th.replay_timer)
>> delete(th)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文