【问题标题】:Callback Functions using Timers in MatlabMatlab中使用定时器的回调函数
【发布时间】:2011-10-20 00:26:58
【问题描述】:

我正在 MATLAB 中研究内容分发服务器的统计模型,并决定使用 OO 编程。这是我第一次使用 MATLAB 涉足 OO,但遇到了障碍。我正在尝试对与服务器的下载连接进行建模,目前它只是一个 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

【问题讨论】:

    标签: matlab matlab-class


    【解决方案1】:

    我解决了我遇到的问题,问题在于声明回调处理程序的方式。我不确定确切的原因,但如果有人感兴趣,这里有更好的解释,请参阅这篇博文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。

    【讨论】:

    • 我不明白您的代码中的dl 是什么,但我使用它并设法使它工作。感谢这条线索:{&lt;handle&gt;}(见我的回答)。顺便说一句,链接中的解决方案仅适用于静态方法。
    【解决方案2】:

    我的猜测是,回调需要是一个静态类函数,并且参数列表需要带有适当的计时器参数。然后静态类回调需要定位对象引用以设置实例isActive 标志。 findobj 可能会按名称获取类对象实例,因为您选择使用句柄对象,但这可能会影响实时响应。

    this.ttl = timer('TimerFcn', @dl.killConnection, 'StartDelay',1); 
    
    
    methods(Static)
          function killConnection(obj, event, string_arg)
            ...
          end
    end
    

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

    【讨论】:

    • 我尝试了您的建议,但没有成功。我得到一个“未定义的函数或方法 'dl.killConnection' 对于类型为'timer'的输入参数”错误返回。这是我一直遇到的同样问题,我无法让它识别我想用作回调的函数。
    【解决方案3】:

    ---- 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多