开发者

Callback Functions using Timers in Matlab

开发者 https://www.devze.com 2023-03-25 01:37 出处:网络
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开发者_JAVA技巧 into OO with MATLAB and I have hit a snag.

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开发者_JAVA技巧 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


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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消