обратные вызовы, таймеры и методы ООП: помощь в использовании метода в качестве обратного вызова таймера - PullRequest
0 голосов
/ 22 апреля 2011

мир волшебников Matlab.

6 лет пользователь Matlab, только что начал играть с ООП в Matlab.Я пытаюсь использовать метод в качестве обратного вызова для таймера, который находится в конструкторе, и он не работает.Что мне не хватает?Пожалуйста, просветите меня.

Спасибо


classdef Vehicle
% Vehicle
% Vehicle superclass

properties
    Speed             % [Km/Hour]
    Length            % [Meter]
    Width             % [Meter]
    Driver            % Properties of the driver
    Current_Route     % Current route
    Route_Percent     % Which Percent of the route the vehicle is in
    Route_Direction   % Driving forward or backeard on the routhe
    Last_Action_Time  % The time stamp for the last action of the vehicle
end

methods
    function this = Vehicle(varargin)
        this.Speed            = varargin{1}; % The speed of the car
        this.Current_Route    = varargin{2}; % What route the vehicle is on
        this.Route_Percent    = varargin{3}; % Where on the routeh is the vehicle
        this.Route_Direction  = varargin{4}; % To what direction is the car moving on the route
        this.Last_Action_Time = clock;       % Set the time the thisect was creted
        Progress_Timer = timer('TimerFcn', @Progress, 'Period', varargin{4}, 'ExecutionMode' ,'FixedRate'); % Sets a timer for progressing vehicle position
        start(Progress_Timer) % Starts the timer
    end

    function this = Progress(this)
        if (this.Route_Percent >= 0 && this.Route_Percent <= 100) % If the vehicle does not exceed the route map
            this.Route_Percent = this.Route_Percent + ...
                this.Route_Direction * this.Speed * etime(clock,this.Last_Action_Time)/3600 / this.Current_Route.Length * 100
            this.Last_Action_Time = clock; % Set the time the progress was done
        else
            this.delete;           % Vehicle is no longer relevant
            stop(Progress_Timer);  % Stops the timer
        end
    end
end
end 

1 Ответ

1 голос
/ 22 апреля 2011

Методы не похожи на подфункции.Это означает, что вы хотите передать объект функции таймера.

Progress_Timer = timer('TimerFcn',@()Progress(this))

Кроме того, вы хотите, чтобы vehicle был классом дескриптора, чтобы он this в дескрипторе функции обновлялся всякий раз, когда объектобновляется.

Однако вместо использования таймера вы можете вместо этого использовать set-методы, которые обновляют состояние транспортного средства при каждом изменении одного из свойств.Это будет намного прозрачнее.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...