我正在 MATLAB 中研究内容分发服务器的统计模型,并决定使用 OO 编程。这是我第一次使用 MATLAB 涉足面向对象,但我遇到了障碍。我正在尝试对服务器的下载连接进行建模,目前它只是一个 MATLAB 计时器和一个 bool 值。当计时器到期时,我想将 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
最佳答案
我解决了我遇到的问题,问题出在回调处理程序的声明方式上。我不确定确切的原因,但如果有人感兴趣,这里有更好的解释,请参阅此博客文章 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。
关于matlab - Matlab 中使用定时器的回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6942355/